Typescript: Support compatibility with previous TS versions for npm packages

Created on 25 Nov 2020  路  2Comments  路  Source: microsoft/TypeScript

Search Terms

compatibility previous typescript versions

Suggestion

Allow me to use the latest and greatest version of Typescript but allow me to specify the minimum TS version I want the emitted declaration files to be compatible with.

Use Cases

For example I might want to use the latest version of Typescript so that I could use the // @ts-expect-error in my tests but I might want my compiled package to be usable with TS 3.1. The emitted declaration files would not include this so would be compatible with 3.1

Examples

Latest and greatest features of TS could be used freely anywhere in the code. As soon as this results in incompatible types being emitted in the declaration files TS throws an error saying this is incompatible.
This would allow new features to be used within a function (where it will not affect the declaration files) but if you wanted to return an incompatible type you would be stopped.

Checklist

My suggestion meets these guidelines:

  • [x] This wouldn't be a breaking change in existing TypeScript/JavaScript code
  • [x] This wouldn't change the runtime behavior of existing JavaScript code
  • [x] This could be implemented without emitting different JS based on the types of the expressions
  • [x] This isn't a runtime feature (e.g. library functionality, non-ECMAScript syntax with JavaScript output, etc.)
  • [x] This feature would agree with the rest of TypeScript's Design Goals.
Declined Suggestion

Most helpful comment

You can achieve this already by providing different .d.ts files for different TypeScript versions: https://www.typescriptlang.org/docs/handbook/declaration-files/publishing.html#version-selection-with-typesversions

You can use the downlevel-dts tool by Nathan to create .d.ts files targeting older versions: https://github.com/sandersn/downlevel-dts

All 2 comments

You can achieve this already by providing different .d.ts files for different TypeScript versions: https://www.typescriptlang.org/docs/handbook/declaration-files/publishing.html#version-selection-with-typesversions

You can use the downlevel-dts tool by Nathan to create .d.ts files targeting older versions: https://github.com/sandersn/downlevel-dts

The current solution as described by @MartinJohns is as far as we intend to go in this area.

Was this page helpful?
0 / 5 - 0 ratings