Types and statements
Type builders
The c.ts namespace builds type nodes. Every builder returns Ty.
| Builder | Result |
|---|---|
c.ts.string, number, boolean, unknown, never | the keyword types |
c.ts.voidType, nullType, undefinedType | void, 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.uniqueSymbol | unique symbol |
A member is { name, type, optional?, readonly? }.
Statements
| Builder | Result |
|---|---|
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
| Builder | Result |
|---|---|
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.