Skip to content

TypeScript Target Overview

Genotype generates idiomatic TypeScript code that can be used directly in the application or published as a package.

This overview shows how Genotype types translate to TypeScript, target-specific features and configuration options.

See the Quick Genotype Language Tour for the language overview and TypeScript Configuration for detailed configuration reference.

The following examples give an overview of how Genotype translates into TypeScript.

This source combines an object, a literal field, an optional field, a record, and any:

Book: {
kind: "book",
displayTitle: string,
subtitle?: string,
ratings: { []: int },
extra: any,
}

The generated module includes its definitions and any required imports:

export interface Book {
kind: "book";
displayTitle: string;
subtitle?: string | undefined;
ratings: Record<string, number>;
extra: any;
}

Genotype unions translate directly to TypeScript union types.

Value: string | int
export type Value = string | number;

Genotype number translates directly to the TypeScript number type.

Amount: number
export type Amount = number;

Genotype int translates into the TypeScript umbrella number type.

Count: int
export type Count = number;

Genotype float translates into the TypeScript umbrella number type.

Ratio: float
export type Ratio = number;

Sized Genotype numeric types translate into the TypeScript umbrella number type, except for i128 and u128, which translate into bigint.

SmallCount: i16
PreciseRatio: f32
LargeCount: i128
export type SmallCount = number;
export type PreciseRatio = number;
export type LargeCount = bigint;

Genotype boolean translates directly to the TypeScript boolean type.

Ready: boolean
export type Ready = boolean;

Genotype string translates directly to the TypeScript string type.

Title: string
export type Title = string;

Genotype literal types translate directly to TypeScript literal types.

Category: "fiction"
export type Category = "fiction";

Genotype null translates directly to the TypeScript null type.

Empty: null
export type Empty = null;

Genotype branded primitives translate into branded TypeScript types. Their runtime values remain unchanged.

BookId: @string
export type BookId = string & { [bookIdBrand]: true };
declare const bookIdBrand: unique symbol;

Genotype objects translate into TypeScript interfaces or type aliases. Choose the output style with ts.prefer.

Book: { title: string }
export interface Book {
title: string;
}

Optional Genotype object fields translate into optional TypeScript properties.

Draft: { subtitle?: string }
export interface Draft {
subtitle?: string | undefined;
}

Genotype object extensions translate into interface inheritance or type intersections.

Named: { name: string }
NamedBook: { ...Named, pages: int }
export interface Named {
name: string;
}
export interface NamedBook extends Named {
pages: number;
}

Genotype arrays translate into TypeScript Array<T> types.

Titles: [string]
export type Titles = Array<string>;

Genotype tuples translate directly to TypeScript tuple types.

Point: (float, float)
export type Point = [number, number];

Genotype records translate into TypeScript Record<K, V> types. An omitted key type means string keys.

Scores: { []: int }
export type Scores = Record<string, number>;

Genotype any translates directly to the TypeScript any type.

Payload: any
export type Payload = any;

Genotype generic types translate into TypeScript generic types. Zod output uses functions accepting schemas, e.g., Envelope(Book).

Envelope<Body>: { body: Body }
export interface Envelope<Body> {
body: Body;
}

Genotype recursive types translate directly to TypeScript recursive types. Zod output uses getters or lazy schemas.

LinkedNode: { value: string, next?: LinkedNode }
export interface LinkedNode {
value: string;
next?: LinkedNode | undefined;
}

Annotations intended for other targets, such as Rust enum variant names, don’t affect TypeScript output.

Use [ts] in genotype.toml to configure TypeScript. See the TypeScript Configuration Reference for more details.

See Genotype Configuration for global settings and Common Target Options.