Skip to main content

Types and statements

Type builders​

The c.ts namespace builds type nodes. Every builder returns Ty.

BuilderResult
c.ts.string, number, boolean, unknown, neverthe keyword types
c.ts.voidType, nullType, undefinedTypevoid, null, undefined
c.ts.ref("Name", ...args)Name or Name<A, B>
c.ts.union(...members)A | B; one member is returned as is, none gives never
c.ts.nullable(type)type | null
c.ts.array(type), readonlyArray(type)type[], ReadonlyArray<type>
c.ts.stringLiteral("a")"a"
c.ts.typeOf("Users", "Type")typeof Users.Type
c.ts.objectType(members){ name?: type; readonly other: type }
c.ts.uniqueSymbolunique symbol

A member is { name, type, optional?, readonly? }.

Statements​

BuilderResult
c.constant(name, init, type?)const name: type = init
c.letting(name, init, type?)let name: type = init
c.ret(expr)return expr
c.exprStmt(expr)an expression statement
c.ifThen(test, ...body)if (test) { ... }

Exports​

BuilderResult
c.exportConst(name, init, type?)export const name = init
c.exportType(name, type)export type Name = type
c.exportInterface(name, members)export interface Name { ... }, with quoted keys when needed
c.exportClass(name, superClass)export class Name extends superClass {}
c.print([
c.exportType("Role", c.ts.union(c.ts.stringLiteral("admin"), c.ts.stringLiteral("user"))),
c.exportInterface("Users", [
{ name: "id", type: c.ts.string },
{ name: "bio", type: c.ts.nullable(c.ts.string), optional: true },
]),
])

c.print(statements) joins the statements into one module with two-space indentation and double quotes. It adds no import declarations; the symbol registry writes them.