Gathering detailed insights and metrics for downlevel-dts
Gathering detailed insights and metrics for downlevel-dts
Gathering detailed insights and metrics for downlevel-dts
Gathering detailed insights and metrics for downlevel-dts
Convert a new d.ts to one that works with older versions of Typescript
npm install downlevel-dts
Module System
Min. Node Version
Typescript Support
Node Version
NPM Version
229 Stars
64 Commits
21 Forks
7 Watching
4 Branches
17 Contributors
Updated on 25 Oct 2024
JavaScript (100%)
Cumulative downloads
Total Downloads
Last day
-12.2%
26,644
Compared to previous day
Last week
-1.2%
164,189
Compared to previous week
Last month
4.8%
742,520
Compared to previous month
Last year
76.1%
6,964,558
Compared to previous year
3
7
downlevel-dts rewrites .d.ts files created by any version of TypeScript so that they work with TypeScript 3.4 or later. It does this by converting code with new features into code that uses equivalent old features. For example, it rewrites accessors to properties, because TypeScript didn't support accessors in .d.ts files until 3.6:
1declare class C { 2 get x(): number; 3}
becomes
1declare class C { 2 readonly x: number; 3}
Note that not all features can be downlevelled. For example, TypeScript 4.0 allows spreading multiple tuple type variables, at any position in a tuple. This is not allowed in previous versions, but has no obvious downlevel emit, so downlevel-dts doesn't attempt to do anything. Be sure to test the output of downlevel-dts with the appropriate version of TypeScript.
Here is the list of features that are downlevelled:
Omit
(3.5)1type Less = Omit<T, K>;
becomes
1type Less = Pick<T, Exclude<keyof T, K>>;
Omit
has had non-builtin implementations since TypeScript 2.2, but
became built-in in TypeScript 3.5.
Omit
is a type alias, so the downlevel should behave exactly the same.
TypeScript prevented accessors from being in .d.ts files until TypeScript 3.6 because they behave very similarly to properties. However, they behave differently with inheritance, so the distinction can be useful.
1declare class C { 2 get x(): number; 3}
becomes
1declare class C { 2 readonly x: number; 3}
The properties emitted downlevel can be overridden in more cases than the original accessors, so the downlevel d.ts will be less strict. See the TypeScript 3.7 release notes for more detail.
asserts
assertion guards (3.7)TypeScript 3.7 introduced the asserts
keyword, which provides a way to indicate that a function will throw if a parameter doesn't meet a condition.
This allows TypeScript to understand that whatever condition such a function checks must be true for the remainder of the containing scope.
Since there is no way to model this before 3.7, such functions are downlevelled to return void
:
1declare function assertIsString(val: any, msg?: string): asserts val is string;
2declare function assert(val: any, msg?: string): asserts val;
becomes
1declare function assertIsString(val: any, msg?: string): void;
2declare function assert(val: any, msg?: string): void;
The downlevel emit is quite simple:
1import type { T } from 'x';
becomes
1import { T } from "x";
The downlevel d.ts will be less strict because a class will be constructable:
1declare class C { 2} 3export type { C };
becomes
1declare class C {} 2export { C };
and the latter allows construction:
1import { C } from "x"; 2var c = new C();
type
modifiers on import/export names (4.5)The downlevel emit depends on the TypeScript target version and whether type and value imports/exports are mixed.
An import/export declaration with only import/export names that have type
modifiers
1import { type A, type B } from "x"; 2export { type A, type B };
becomes:
1// TS 3.8+ 2import type { A, B } from "x"; 3export type { A, B }; 4 5// TS 3.7 or less 6import { A, B } from "x"; 7export { A, B };
A mixed import/export declaration
1import { A, type B } from "x"; 2export { A, type B };
becomes:
1// TS 3.8+ 2import type { B } from "x"; 3import { A } from "x"; 4export type { B }; 5export { A }; 6 7// TS 3.7 or less 8import { A, B } from "x"; 9export { A, B };
When an import/export declaration has only import/export names with type
modifiers, it is emitted as a type-only import/export declaration for TS 3.8+
and as a value import/export declaration for TS 3.7 or less. The latter will be
less strict (see type-only import/export).
When type and value imports/exports are mixed, two import/export declarations are emitted for TS 3.8+, one for type-only imports/exports and another one for value imports/exports. For TS 3.7 or less, one value import/export declaration is emitted which will be less strict (see type-only import/export).
#private
(3.8)TypeScript 3.8 supports the new ECMAScript-standard #private properties in addition to its compile-time-only private properties. Since neither are accessible at compile-time, downlevel-dts converts #private properties to compile-time private properties:
1declare class C { 2 #private 3}
It becomes:
1declare class C { 2 private "#private"` 3}
The standard emit for any class with a #private property just adds a
single #private
line. Similarly, a class with a private property
adds only the name of the property, but not the type. The d.ts
includes only enough information for consumers to avoid interfering
with the private property:
1class C { 2 #x = 1 3 private y = 2 4}
emits
1declare class C { 2 #private 3 private y 4}
which then downlevels to
1declare class C { 2 private "#private"; 3 private y; 4}
This is incorrect if your class already has a field named "#private"
.
But you really shouldn't do this!
The downlevel d.ts incorrectly prevents consumers from creating a
private property themselves named "#private"
. The consumers of the
d.ts also shouldn't do this.
export * from 'x'
(3.8)TypeScript 3.8 supports the new ECMAScript-standard export * as namespace
syntax, which is just syntactic sugar for two import/export
statements:
1export * as ns from 'x';
becomes
1import * as ns_1 from "x"; 2export { ns_1 as ns };
The downlevel semantics should be exactly the same as the original.
[named: number, tuple: string, ...members: boolean[]]
(4.0)TypeScript 4.0 supports naming tuple members:
1type T = [foo: number, bar: string];
becomes
1type T = [/** foo */ number, /** bar */ string];
The downlevel semantics are exactly the same as the original, but the TypeScript language service won't be able to show the member names.
in out T
(4.7)Typescript 4.7 supports variance annotations on type parameter declarations:
1interface State<in out T> { 2 get: () => T; 3 set: (value: T) => void; 4}
becomes:
1interface State<T> { 2 get: () => T; 3 set: (value: T) => void; 4}
The downlevel .d.ts omits the variance annotations, which will change the variance in the cases where they were added because the compiler gets it wrong.
Since the earliest downlevel feature is from TypeScript 3.5,
downlevel-dts targets TypeScript 3.4 by default. The downlevel target is
configurable with --to
argument.
Currently, TypeScript 3.0 features like unknown
are not
downlevelled, nor are there any other plans to support TypeScript 2.x.
$ npm install downlevel-dts
$ npx downlevel-dts . ts3.4 [--to=3.4]
1"typesVersions": { 2 "<4.0": { "*": ["ts3.4/*"] } 3}
$ cp tsconfig.json ts3.9/tsconfig.json
These instructions are modified and simplified from the Definitely Typed ones.
No vulnerabilities found.
No security vulnerabilities found.