Skip to content

Commit fc559a8

Browse files
authored
fix: deep-import of index files breaks ts3.9 compilers (#81)
The `typesVersions` directive applies recursively when the TypeScript compiler searches for the appropriate object to load... When attempting to resolve a directory, it will first rewrite that directory according to the typesVersions directive, and then it will rewrite the `index` sub-path within there again, resulting in two distinct rewrites happening within the same resolution. This appears to be a TypeScript bug as reported in microsoft/TypeScript#43133. The work-around is to include a second rewrite candidate that explicitly targets the `*/index.d.ts` sub-path, which removes the need for a rewrite when searching down the `index` route. Causes projen/projen#2570 --- By submitting this pull request, I confirm that my contribution is made under the terms of the [Apache 2.0 license]. [Apache 2.0 license]: https://www.apache.org/licenses/LICENSE-2.0
1 parent f0d5b10 commit fc559a8

File tree

8 files changed

+84
-34
lines changed

8 files changed

+84
-34
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export class BarrelImportClass {}

fixtures/@scope/jsii-calc-base/lib/index.ts

+2
Original file line numberDiff line numberDiff line change
@@ -35,3 +35,5 @@ export class StaticConsumer {
3535
return StaticConsumerBase.consume(...args);
3636
}
3737
}
38+
39+
export * as deep from './deep';

fixtures/@scope/jsii-calc-base/package.json

+11-1
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,20 @@
2626
"typesVersions": {
2727
"<=3.9": {
2828
"*": [
29-
".types-compat/ts3.9/*"
29+
".types-compat/ts3.9/*",
30+
".types-compat/ts3.9/*/index.d.ts"
3031
]
3132
}
3233
},
34+
"exports": {
35+
".": {
36+
"types": "./lib/index.d.ts",
37+
"import": "./lib/index.js",
38+
"require": "./lib/index.js"
39+
},
40+
"./lib/deep": "./lib/deep.js",
41+
"./.warnings.jsii.js": "./.warnings.jsii.js"
42+
},
3343
"dependencies": {
3444
"@scope/jsii-calc-base-of-base": "^2.1.1"
3545
},

fixtures/@scope/jsii-calc-lib/lib/index.ts

+4
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import * as base from '@scope/jsii-calc-base';
2+
import * as deep from '@scope/jsii-calc-base/lib/deep';
23
import { Very } from '@scope/jsii-calc-base-of-base';
34

45
/**
@@ -124,6 +125,9 @@ export class BaseFor2647 {
124125

125126
public foo(obj: base.IBaseInterface): void {
126127
obj.bar();
128+
129+
// Just so it's used... no other interest here.
130+
new deep.BarrelImportClass();
127131
}
128132
}
129133

fixtures/jsii-calc/package.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,8 @@
3535
"typesVersions": {
3636
"<=3.9": {
3737
"*": [
38-
".types-compat/ts3.9/*"
38+
".types-compat/ts3.9/*",
39+
".types-compat/ts3.9/*/index.d.ts"
3940
]
4041
}
4142
},

src/downlevel-dts.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,8 @@ export function emitDownleveledDeclarations({ packageJson, projectRoot, tsc }: P
115115
typesVersions ??= {};
116116
const from = [...(tsc?.outDir != null ? [tsc?.outDir] : []), '*'].join('/');
117117
const to = [...(tsc?.outDir != null ? [tsc?.outDir] : []), TYPES_COMPAT, versionSuffix, '*'].join('/');
118-
typesVersions[`<=${version}`] = { [from]: [to] };
118+
// We put 2 candidate redirects (first match wins), so that it works for nested imports, too (see: https://github.com/microsoft/TypeScript/issues/43133)
119+
typesVersions[`<=${version}`] = { [from]: [to, `${to}/index.d.ts`] };
119120
}
120121

121122
// Compare JSON stringifications, as the order of keys is important here...

test/__snapshots__/integration.test.ts.snap

+59-31
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)