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 = 15How 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 print105. Keeptsc --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]),namespaceand parameter properties likeconstructor(private n: number)all ran correctly in a quick check. - Multi-file TypeScript needs
--allow-readfor the imported files, exactly like JavaScript (see 11 · React Native). Bothimport "./state"andimport "./state.ts"resolve. - The same transpiler is used by
3va test,3va devand3va bundle, so.tsworks the same everywhere.