How do I use the typesVersions field in package.json to ship stricter types for newer typescript versions?

Viewed 3082

According to the TypeScript 3.1 release notes, typescript provides a way to ship stricter types in npm packages by setting the typesVersions field in package.json like so:

{
  "name": "package-name",
  "version": "1.0",
  "types": "./index.d.ts",
  "typesVersions": {
    ">=3.1": { "*": ["ts3.1/*"] }
  }
}

However, the docs do not indicate how the specified ts3.1/index.d.ts file is created. Do I have to manually write an index.d.ts file at this location? Is there a way with typescript or a related build tool to conditionally generate stricter types?

1 Answers

Update

Looks like a TS team member has just started working on a tool for this (maybe a side project). As of writing it doesn't appear to be finished but definitely worth watching. https://github.com/sandersn/downlevel-dts

Original answer

As far as I can find, nobody has written a tool to automatically generate these files yet. Most recent public word on this from a TypeScript team member is that they don't intend to add this as a compiler feature. It was also requested here with no response. So you may be stuck translating manually for now.

(Conceptually, a tool to do this would be a lot like "Babel for typings": eliminating new syntax/keywords and "polyfilling" or eliminating new type helpers. It might even be possible to write as a Babel plugin.)

Related