Skip to content

TypeScript build setup

TypeScript erases type information at runtime. Aster’s aster-gen scanner reads your source at build time via the TypeScript compiler API, discovers all @Service, @WireType, @Rpc, @ServerStream, @ClientStream, and @BidiStream decorated classes, and emits aster-rpc.generated.ts with the metadata the runtime needs — field shapes, wire type mappings, method signatures, and contract identity hashes.

This replaces the need to pass { request, response } to every decorator. The scanner extracts request and response types from the AST.

Install the public preview from Forgejo packages. Configure the scoped registry before installing.

Add this scope mapping to your project’s .npmrc:

@aster-rpc:registry=https://forge.emrul.dev/api/packages/Aster/npm/

Then install the pinned preview and TypeScript compiler:

Terminal window
npm install @aster-rpc/aster@0.3.893
npm install --save-dev typescript@^5 @types/node

aster-gen ships as the aster-gen bin inside @aster-rpc/aster — no extra package needed. TypeScript is a peer dependency (bring your own version).

Set "type": "module" in package.json. For these examples, use this tsconfig.json so JavaScript and generated metadata are emitted beside their TypeScript source files:

{
"compilerOptions": {
"target": "ES2022",
"module": "Node16",
"moduleResolution": "Node16",
"strict": true,
"skipLibCheck": true
},
"include": ["*.ts"]
}

Leave experimentalDecorators unset. Run npx aster-gen, then npx tsc, and run the emitted .js files with Node.js. If you use a separate output directory, run from that directory so the generated metadata is available in the working directory, or pass it explicitly to AsterServer.

Terminal window
# defaults: reads ./tsconfig.json, writes ./aster-rpc.generated.ts
npx aster-gen
# custom paths
npx aster-gen -p tsconfig.app.json -o build/aster-rpc.generated.ts

Or add it to your package.json:

{
"scripts": {
"build": "aster-gen && tsc"
}
}

Re-run after adding or changing wire types, RPC methods, or decorator options. The generated file is deterministic — same input always produces the same output.

AsterServer.start() auto-imports aster-rpc.generated.js from the working directory — no manual import or wiring needed:

import { AsterServer } from '@aster-rpc/aster';
import { MyService } from './services.js';
const server = new AsterServer({
services: [new MyService()],
});
await server.start();
await server.serve();

If the generated file is missing, the runtime falls back to reflection and logs a warning. That fallback cannot recover every schema shape and does not produce matching cross-language contract hashes; use generated metadata for typed interoperability.

The output aster-rpc.generated.ts contains two exports:

  • WIRE_TYPES — ordered array of wire type descriptors (tag, fields, nested type refs, field name sets) in dependency order.
  • SERVICES — array of service descriptors with method signatures, RPC patterns, pre-derived manifest fields, and BLAKE3 type hashes for cross-language contract identity.

The file is // @ts-nocheck and auto-formatted. Commit it or gitignore it — either works. Committing preserves scanner output for review, but the generated TypeScript still needs compilation. If you ignore it, generate it before every build.

For projects using a bundler, plugins run aster-gen automatically on build and during hot reload. When using a bundler plugin, pass the generated option explicitly since dynamic import won’t resolve at bundle time:

vite.config.ts
import { defineConfig } from 'vite';
import { asterGen } from '@aster-rpc/aster/vite-plugin';
export default defineConfig({
plugins: [
asterGen({
project: 'tsconfig.json',
out: 'aster-rpc.generated.ts',
}),
],
});

The plugin runs on buildStart and watches for changes to decorated source files during dev.

webpack.config.ts
import { AsterGenWebpackPlugin } from '@aster-rpc/aster/webpack-plugin';
export default {
plugins: [
new AsterGenWebpackPlugin({
project: 'tsconfig.json',
out: 'aster-rpc.generated.ts',
}),
],
};

The plugin hooks into beforeCompile and re-scans when source files change.

The scanner maps TypeScript types to wire types per the Aster spec:

TypeScript Wire type
string string
boolean bool
number float64
bigint int64
Date timestamp
Uint8Array binary
T[] / Array<T> list<T>
Set<T> set<T>
Map<K, V> map<K, V>
T | null / T | undefined nullable<T>
Class with @WireType ref (by tag)

Plain number maps to float64. For narrower types (int32, uint16, etc.), use branded types:

import { WireType, i32, u64, f32 } from '@aster-rpc/aster';
@WireType("myapp/Metrics")
class Metrics {
count: i32 = 0 as i32; // → int32
total_bytes: u64 = 0n as u64; // → uint64
avg_latency: f32 = 0 as f32; // → float32
}

The scanner detects branded types via their phantom property and maps them to the correct wire primitive.

If aster-rpc.generated.js is not found (e.g. during rapid prototyping), the runtime falls back to reflection: instantiating each wire type class and inspecting Object.keys(). This works for simple cases but has limitations:

  • Empty arrays don’t reveal their element type.
  • Optional/nullable nested types aren’t recursed.
  • Non-default-constructible classes can’t be introspected.
  • Contract identity hashes are zeroed (cross-language calls won’t match).

A console warning fires once per class on the fallback path. For production use, always run aster-gen.

With the compiler configuration above, keep services.ts, producer.ts, and aster-rpc.generated.ts in the project directory. After npm run build, expect matching services.js, producer.js, and aster-rpc.generated.js beside them. Run node producer.js from that directory so the default metadata import resolves.

If you set outDir to dist, inspect that it contains both application JavaScript and generated metadata, then run from dist or pass the generated descriptors explicitly. A successful scanner run alone does not compile either file. If runtime warns about reflection fallback, fix metadata emission/loading before diagnosing a cross-language contract mismatch.