Skip to content

This notebook belongs to

Sadat Jubayer

Senior Software Engineer at Premio

If found, please return to hello@jubayer.dev

Vol. 7Seven years of building

Started
January 2020
Everyday tools
TypeScript, Vue, React, Next.js, NestJS, React Native
Education
B.Sc. CSSE, AIUB, 2020
1 min readTypeScript

erasableSyntaxOnly for Node type stripping

Turn on erasableSyntaxOnly to catch enums and other TypeScript syntax that Node cannot run by stripping types.

You move a small task script from a TypeScript build to Node's built-in type stripping. Save this as status.ts and run node status.ts on Node 22.18 or later in the 22 line.

enum Status {
  Queued = 'queued',
  Done = 'done',
}

const status: Status = Status.Done
console.log(status)

With type stripping, Node reports ERR_UNSUPPORTED_TYPESCRIPT_SYNTAX at enum Status.

Node can erase the type annotation : Status. The enum declaration needs emitted JavaScript because Status.Done reads a property at runtime.

Make the checker flag the enum

Put this in tsconfig.json beside status.ts. Run pnpm exec tsc -p . in that directory with TypeScript 5.8 or later installed.

{
  "compilerOptions": {
    "module": "nodenext",
    "noEmit": true,
    "erasableSyntaxOnly": true,
    "verbatimModuleSyntax": true
  },
  "files": ["status.ts"]
}

The checker reports TS1294 on the enum before you run the script. TypeScript's flag also flags parameter properties and namespaces with runtime code.

noEmit makes this a check. The Node type stripper ignores tsconfig and does no type checking. Run the checker in CI to catch this before the script runs.

Keep the runtime value in JavaScript

Replace the enum with a plain object and derive the type from its values.

const Status = {
  Queued: 'queued',
  Done: 'done',
} as const

type Status = (typeof Status)[keyof typeof Status]
const status: Status = Status.Done
console.log(status)

Run the checker again, then run node status.ts. The checker passes and Node prints done.

The JavaScript object supplies the property at runtime. The as const assertion preserves each value's literal type for the checker. The type alias then admits the strings 'queued' and 'done' where you annotate a value as Status.

For a type imported from another file, write import type so Node can erase the whole import. With verbatimModuleSyntax, the checker can catch a type imported as a value. For an import that loads another local TypeScript file at runtime, spell out its .ts extension and enable allowImportingTsExtensions in the no-emit config.