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

cts-types

v0.0.6

Published

A command line script for generating d.cts files from d.ts files.

Downloads

122

Readme

cts-types

A command line script for generating d.cts files from d.ts files.

npm version License: MIT

Supporting esmodules and commonjs in the same Typescript library that is itself an esmodule is not straight forward. At the time of creating this library, Typescript's new module resolution strategies requires esmodule libraries that support commonjs to provide a set of types suffixed with d.cts instead of d.ts.

Without d.cts files, Typescript assumes the library only supports esmodules and throws an error when you try to require() the commonjs version of the library. For more information about this, read the Github issue about NodeNext duel package resolution.

cts-types streamlines the process of generating d.cts files. Just install it as a dev dependency and run the cts-types build command in your pipeline after generating the d.ts files. The script will not just copy and rename files, it will also update references to .d.cts and .d.cts.map in the content of a file.

Install package

# terminal
npm add cts-types --save-dev

Usage

// package.json
{
  ...
  "types": "./dist/types/cjs/index.d.cts",
  "exports": {
    "types": {
      "import": "./dist/types/esm/index.d.ts",
      "require": "./dist/types/cjs/index.d.cts"
    },
    ...
  },
  ...
  "scripts": {
    ...
    "compile:types": "tsc --project ./tsconfig.build.json && cts-types build dist/types/esm dist/types/cjs",
    ...
  }
  ...
}
// tsconfig.build.json
{
  ...
  "compilerOptions": {
    ...
    "outDir": "dist/types/esm"
    ...
  },
  ...
}

API

ctsTypes build <input> <output>

Build .d.cts type files from .d.ts files to provide types for both esm and cjs outputs.

Positionals:
  input   The directory where the .d.ts files are located, relative to the
          project root                                       [string] [required]
  output  The directory where the .d.cts files should be output, relative to the
          project root                                       [string] [required]

Options:
  --version  Show version number                                       [boolean]
  --help     Show help                                                 [boolean]
  --verbose  Whether to output verbose logs                            [boolean]

A note on output

The output should be in the same folder as or a sibling folder of the input in order for the source maps to work correctly. The source maps include a path to the source file and that path is based on the location of the input file. This library does not currently support updating the relative path of source files in the .map files.

To get around this limitation, either do not provide an output path (and the output will be written to the input path) or have the output path leaf directory be a sibling of the input path leaf directory, i.e. input of dist/types/esm and output of dist/types/cjs.

Changelog

Check out the features, fixes and more that go into each major, minor and patch version.

License

cts-types is MIT Licensed.