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

Source

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).

See also

On this page