Skip to Content
Examples04 · TypeScript

04 · TypeScript

A calculator class with an interface, a private field, a readonly return type and a generic helper. You run the .ts file directly.

Source: examples/04-typescript

Run

cd examples/04-typescript 3va run math.ts
=== TypeScript in 3va === 10 + 5 = 15 10 - 3 = 7 4 * 7 = 28 20 / 6 = 3.3333 History: 10 + 5 = 15 10 - 3 = 7 4 * 7 = 28 20 / 6 = 3.3333333333333335 First result: 10 + 5 = 15

How it works

3va strips the TypeScript syntax with oxc  when it loads the file, then runs the resulting JavaScript. interface, type annotations, generics (first<T>), private and implements are removed, and the runtime code stays the same. There’s no tsconfig.json and no output directory.

Things to keep in mind

  • Type-stripping is not type-checking. Code with type errors still runs. If calc.add("10", 5) slipped in, 3va would happily print 105. Keep tsc --noEmit (or your editor) in CI for type safety.
  • Features that generate runtime code work too. This example doesn’t use them, but enum (including reverse mapping, Color[0]), namespace and parameter properties like constructor(private n: number) all ran correctly in a quick check.
  • Multi-file TypeScript needs --allow-read for the imported files, exactly like JavaScript (see 11 · React Native). Both import "./state" and import "./state.ts" resolve.
  • The same transpiler is used by 3va test, 3va dev and 3va bundle, so .ts works the same everywhere.