LogoPear Docs

bare-type-stripper

Heuristic lexer for stripping TypeScript type syntax to produce plain JavaScript

stable

bare-type-stripper — Heuristic lexer for stripping TypeScript type syntax to produce plain JavaScript. It is a native addon.

npm i bare-type-stripper

Usage

const strip = require('bare-type-stripper')

strip(`
  const x: number = 1
  function f<T>(xs: T[]): T { return xs[0] }
`).toString()

// '
//   const x         = 1
//   function f   (xs   )    { return xs[0] }
// '

API

Functions

strip(input: string | Buffer, encoding?: BufferEncoding, opts?: object): Buffer

Strip TypeScript-only syntax from input and return plain JavaScript as a Buffer. Stripped regions are replaced with spaces (newlines preserved) so the output has the same byte length as the input, keeping stack traces and source positions aligned.

Parameters

ParameterTypeDefaultDescription
inputstring | BufferThe TypeScript source to strip, as a string or a Buffer.
encoding?BufferEncodingEncoding used to decode input when it is a string (default 'utf8'); ignored when input is already a Buffer.
opts?objectAn options object; currently unused.

Throws

  • TypeErrorinput is neither a string nor a buffer.
  • SyntaxError — the source contains non-erasable TypeScript syntax (enum/const enum, namespace/module with a body, parameter properties, or angle-bracket type assertions).

strip.lex

strip.lex(input: string | Buffer, encoding?: BufferEncoding, opts?: object): [start: number, end: number, flags?: number][]

Parameters

ParameterTypeDefaultDescription
inputstring | Buffer
encoding?BufferEncoding
opts?object

Constants and variables

strip.constants

strip.constants: {
    SEMI: number
    PAREN: number
    ERROR: number
  }

What gets stripped

ConstructExample
Type annotationsconst x: number = 1
Type aliasestype Foo = number
Interfacesinterface Foo { x: number }
Type-only imports/exportsimport type { Foo } from 'mod'
Generics at declarationsfunction f<T>(x: T): T
Generics at call sitesfoo<number>()
Generic arrow functions<T>(x: T) => x
Type assertionsx as Foo, x satisfies Foo
Non-null assertionobj!.foo
Optional parameter markerfunction f(x?: T)
Definite assignmentlet x!: number
Class member modifierspublic, private, readonly, etc.
implements clausesclass C implements I
declare statementsdeclare const x: number
Overload signaturesfunction f(x: string): void
Abstract membersabstract foo(): void

What is left alone

  • Decorators - they emit runtime code and are valid JavaScript syntax.

What throws

Constructs with runtime semantics that a purely lexical stripper cannot reproduce are marked with the ERROR flag, and strip() throws a SyntaxError when it meets one:

  • enum / const enum declarations - they emit a runtime object.
  • namespace / module declarations with bodies - they emit runtime code.
  • Parameter properties - constructor(public x: number) implies a this.x = x assignment that stripping the modifier would silently lose.
  • Old-style angle-bracket type assertions (<Foo>expr) - indistinguishable from JSX, which is not supported.

Limitations

The stripper targets plain .ts sources; JSX (.tsx) is not supported and is reported as non-erasable syntax.

See also

On this page