Skip to content

TypeScript target

Use the TypeScript target for browser, Node, or shared package consumers that need generated model classes and helper behavior.

emit-targets:
- type: TypeScript
output-dir: "generated/typescript"
test-dir: "generated/typescript/tests"
import-path: "../index"
protocol-scaffolds: "compile-only"
native-serialization: "zod"
outputs:
- kind: consumer
provider: fetch
OptionWhy it matters
output-dirWhere generated TypeScript source is written.
test-dirWhere generated Node test files are written.
import-pathImport path used by generated tests.
protocol-scaffoldsEmits compile-only test scaffolds for generated protocols.
native-serializationDefaults to "none". Set to "zod" to emit class-attached Zod validators. "standard-schema" is reserved and fails fast.
outputsOptional generated surfaces. kind: consumer / provider: fetch emits contract-specific HTTP clients backed by an injected transport.

Generated TypeScript surfaces include model classes, runtime helpers, JSON/YAML helpers, tests, protocol scaffold tests, optional Zod validation artifacts, and optional fetch consumer clients.

native-serialization: "zod" emits schema, wireSchema, and wireObjectSchemaWithoutName validators on generated classes. schema.parse() canonicalizes through Typra’s generated load(input).save() path before validating the saved wire shape, so Zod validation cannot drift from provider wire names, discriminator dispatch, optional/default handling, or raw fallback preservation.

import { FixtureRoot } from "./generated/typescript";
const root = FixtureRoot.fromJson(jsonText);
const saved = root.toJson();

Review generated tests for the exact helper names emitted by your selected contract and target options.

outputs: [{ kind: consumer, provider: fetch }] emits a transport-client.ts module with contract-specific clients such as PetsClient or RendererClient. The client accepts a baseUrl and an injected TypraFetchTransport, builds paths/query strings/headers/cookies from TypeSpec HTTP metadata, serializes request bodies with generated save() methods when available, forwards contract auth requirements as metadata, checks declared success statuses, and hydrates only status-matched model responses through generated load() methods.

import { PetsClient } from "./generated/typescript/transport-client";
const client = new PetsClient({
baseUrl: "https://api.example.test",
transport: async (request) => {
const headers = { ...request.headers };
if (request.auth) {
headers.Authorization = await buildHostAuthorizationHeader(request.auth);
}
const response = await fetch(request.url, {
method: request.method,
headers,
body: request.body === undefined ? undefined : JSON.stringify(request.body),
});
return {
status: response.status,
body: await response.json(),
};
},
});

The generated client is a contract projection, not a full SDK runtime. Typra does not synthesize Authorization headers or manage credentials. Auth, cookie persistence, retries, tracing, streaming, and host policy stay in the injected transport. A response whose status does not match one of the operation’s declared success statuses is reported through TypraFetchResponseError with the original status/body, even when the status is another 2xx.