npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

detypes

v0.8.0

Published

Removes TypeScript type annotations but keeps the formatting

Downloads

8,385

Readme

detypes

Fork from https://github.com/cyco130/detype, Since the original author has not processed the PR for a long time, a patch version has been released for the time being until original repo is active again.

npm version npm downloads

npm i -g detypes

Suppose you have a library that you want to provide usage examples for. detypes can help you generate vanilla JavaScript samples from TypeScript samples automatically and remove the burden of maintaining two separate versions of what is essentially the same code.

It is a command line tool and a library that removes type annotations and other TypeScript specific syntax constructs and outputs vanilla JavaScript without altering the source formatting too much. It supports .ts, .tsx, as well as .vue files.

In other words, it turns this:

import type { ParsedPath } from 'node:path'

let x: string

// This comment should be kept

// This comment should be deleted
// Ditto for this
interface Foo {
  // This should go too
  bar: number
}

// This comment should also be kept
export function bar(foo: Foo): Date {
  return new Date()
}

into this:

let x

// This comment should be kept

// This comment should also be kept
export function bar(foo) {
  return new Date()
}

The output is very close to hand-written JavaScript, especially if you were already using Prettier for formatting.

Doesn't tsc already do that?

There are lots of tools for transpiling TypeScript into plain JavaScript (tsc, babel, swc, esbuild, sucrase etc.) but none of them is perfectly suitable for this specific use case. Most of them don't preserve the formatting at all. sucrase comes close, but it doesn't remove comments attached to TypeScript-only constructs.

detypes uses Babel, a small Babel plugin to remove comments attached to TypeScript-only constructs, and Prettier under the hood. For Vue files, it also uses the tools from the VueDX project.

Magic comments

Sometimes you want the generated JavaScript to be slightly different than the TypeScript original. You can use the magic comments feature to achieve this:

Input:

// @detypes: replace
// These two lines will be removed
console.log('Hello from TypeScript')
// @detypes: with
// // Notice the double comments!
// console.log("Hello from JavaScript");
// @detypes: end

Output:

// Notice the double comments!
console.log('Hello from JavaScript')

If you just want to remove the magic comments, you can use the -m CLI flag or the removeMagicComments function to generate uncluttered TypeScript like this:

// These two lines will be removed
console.log('Hello from TypeScript')

System requirements

detypes requires Node version 14.19.3 or later.

CLI Usage

  detypes [-m | --remove-magic-comments] <INPUT> [OUTPUT]

    INPUT   Input file or directory

    OUTPUT  Output file or directory
      (optional if it can be inferred and it won't overwrite the source file)

    -t, --remove-ts-comments
      Remove @ts-ignore and @ts-expect-error comments

    -m, --remove-magic-comments
      Remove magic comments only, don't perform ts > js transform

  detypes [-v | --version]

    Print version and exit

  detypes [-h | --help]

    Print this help and exit

Node API

// Transform TypeScript code into vanilla JavaScript without affecting the formatting
function transform(
// Source code
  code: string,
// File name for the source
  fileName: string,
// Options to pass to prettier
  prettierOptions?: PrettierOptions | null,
): Promise<string>

// Transform the input file and write the output to another file
function transformFile(
  inputFileName: string,
  outputFileName: string,
): Promise<void>

// Remove magic comments without performing the TS to JS transform
export function removeMagicComments(
// Source code
  code: string,
// File name for the source
  fileName: string,
// Options to pass to prettier
  prettierOptions?: PrettierOptions | null,
): string

// Remove magic comments from the input file and write the output to another file
export function removeMagicCommentsFromFile(
  inputFileName: string,
  outputFileName: string,
): Promise<void>

Change log

0.6

  • feature: Option to remove @ts-ignore and @ts-expect-error comments
  • fix: Preserve newline runs (especially in template literals!)

0.5

  • BREAKING CHANGE: Drop support for Node 12
  • chore: Set up CI workflows

0.4

  • feature: CLI support for removing magic comments
  • chore: Improve documentation

0.3

  • feature: Magic comments
  • feature: Expose type declarations
  • fix: Better empty line handling

0.2

  • feature: for Vue single file components

0.1

  • Initial release

Credits

Fatih Aygün, under MIT License