TransformerFn is not exported, so a custom @Transformer cannot be typed #66

Closed
opened 2026-08-20 17:53:18 +00:00 by chrnx · 0 comments
Owner

Transformer is exported and TransformerFn is not, so a custom transformer can be attached but not typed.

What happens

src/index.ts exports the decorator:

export { ..., Transformer, Html } from './metadata';
export type { Transport, HttpMethod, RouteDecorator } from './metadata';

TransformerFn — the type of the decorator's only argument — is missing from that second line. In the published dist/index.d.ts it appears twice: once in an internal import, once as the type of JsonTransformer. It is never re-exported.

So this does not compile against the package:

import { Transformer, type TransformerFn } from '@green-tea/core';
//                          ^ has no exported member 'TransformerFn'

and the workaround is to borrow the type off a value:

const prometheusText: typeof JsonTransformer = (value) => ({
  status: 200,
  headers: { 'content-type': 'text/plain; version=0.0.4; charset=utf-8' },
  body: String(value),
});

That works, and it reads like a trick, because it is one.

Why it matters

A custom transformer is the documented way to return anything that is not JSON, and @Transformer is a first-class decorator sitting next to @Get and @Html. Every user who writes one either declares the shape inline — losing the check that the decorator's parameter would have given them — or discovers typeof JsonTransformer.

ErrorRenderer and StreamEncoder, the two neighbouring extension points, are both exported as types. This one looks like an oversight rather than a decision.

Scope

One line in src/index.ts:

export type { Transport, HttpMethod, RouteDecorator, TransformerFn } from './metadata';

Worth checking the same barrel for other extension-point types reachable only through a value.

Related

Found while writing the /metrics route for the green-desk demo — Prometheus scrapes text, so the handler needed its own transformer.

`Transformer` is exported and `TransformerFn` is not, so a custom transformer can be attached but not typed. ## What happens `src/index.ts` exports the decorator: ```ts export { ..., Transformer, Html } from './metadata'; export type { Transport, HttpMethod, RouteDecorator } from './metadata'; ``` `TransformerFn` — the type of the decorator's only argument — is missing from that second line. In the published `dist/index.d.ts` it appears twice: once in an internal import, once as the type of `JsonTransformer`. It is never re-exported. So this does not compile against the package: ```ts import { Transformer, type TransformerFn } from '@green-tea/core'; // ^ has no exported member 'TransformerFn' ``` and the workaround is to borrow the type off a value: ```ts const prometheusText: typeof JsonTransformer = (value) => ({ status: 200, headers: { 'content-type': 'text/plain; version=0.0.4; charset=utf-8' }, body: String(value), }); ``` That works, and it reads like a trick, because it is one. ## Why it matters A custom transformer is the documented way to return anything that is not JSON, and `@Transformer` is a first-class decorator sitting next to `@Get` and `@Html`. Every user who writes one either declares the shape inline — losing the check that the decorator's parameter would have given them — or discovers `typeof JsonTransformer`. `ErrorRenderer` and `StreamEncoder`, the two neighbouring extension points, are both exported as types. This one looks like an oversight rather than a decision. ## Scope One line in `src/index.ts`: ```ts export type { Transport, HttpMethod, RouteDecorator, TransformerFn } from './metadata'; ``` Worth checking the same barrel for other extension-point types reachable only through a value. ## Related Found while writing the `/metrics` route for the `green-desk` demo — Prometheus scrapes text, so the handler needed its own transformer.
chrnx closed this issue 2026-09-04 13:32:28 +00:00
Sign in to join this conversation.