Skip to content

Commit 3bf0d30

Browse files
authored
Pick PR #49356 (Add nightly-only error on ImportType resolution mode assertion) into release-4.7 (#49365)
* Add nightly-only error on ImportType resolution mode assertion (#49356) * Add nightly-only error on ImportType resolution mode assertion * Temporarily change version to demonstrate errors * Revert "Temporarily change version to demonstrate errors" This reverts commit 40c2469. * "Resolution mode" -> "resolution-mode" * Update baselines
1 parent 5f7f0b1 commit 3bf0d30

22 files changed

+257
-102
lines changed

src/compiler/checker.ts

+8-3
Original file line numberDiff line numberDiff line change
@@ -6199,6 +6199,7 @@ namespace ts {
61996199
factory.createStringLiteral("import")
62006200
)
62016201
])));
6202+
context.tracker.reportImportTypeNodeResolutionModeOverride?.();
62026203
}
62036204
}
62046205
if (!specifier) {
@@ -6222,6 +6223,7 @@ namespace ts {
62226223
factory.createStringLiteral(swappedMode === ModuleKind.ESNext ? "import" : "require")
62236224
)
62246225
])));
6226+
context.tracker.reportImportTypeNodeResolutionModeOverride?.();
62256227
}
62266228
}
62276229

@@ -35825,8 +35827,11 @@ namespace ts {
3582535827
if (node.assertions) {
3582635828
const override = getResolutionModeOverrideForClause(node.assertions.assertClause, grammarErrorOnNode);
3582735829
if (override) {
35830+
if (!isNightly()) {
35831+
grammarErrorOnNode(node.assertions.assertClause, Diagnostics.resolution_mode_assertions_are_unstable_Use_nightly_TypeScript_to_silence_this_error_Try_updating_with_npm_install_D_typescript_next);
35832+
}
3582835833
if (getEmitModuleResolutionKind(compilerOptions) !== ModuleResolutionKind.Node16 && getEmitModuleResolutionKind(compilerOptions) !== ModuleResolutionKind.NodeNext) {
35829-
grammarErrorOnNode(node.assertions.assertClause, Diagnostics.Resolution_modes_are_only_supported_when_moduleResolution_is_node16_or_nodenext);
35834+
grammarErrorOnNode(node.assertions.assertClause, Diagnostics.resolution_mode_assertions_are_only_supported_when_moduleResolution_is_node16_or_nodenext);
3583035835
}
3583135836
}
3583235837
}
@@ -40720,11 +40725,11 @@ namespace ts {
4072040725
const override = getResolutionModeOverrideForClause(declaration.assertClause, validForTypeAssertions ? grammarErrorOnNode : undefined);
4072140726
if (validForTypeAssertions && override) {
4072240727
if (!isNightly()) {
40723-
grammarErrorOnNode(declaration.assertClause, Diagnostics.Resolution_mode_assertions_are_unstable_Use_nightly_TypeScript_to_silence_this_error_Try_updating_with_npm_install_D_typescript_next);
40728+
grammarErrorOnNode(declaration.assertClause, Diagnostics.resolution_mode_assertions_are_unstable_Use_nightly_TypeScript_to_silence_this_error_Try_updating_with_npm_install_D_typescript_next);
4072440729
}
4072540730

4072640731
if (getEmitModuleResolutionKind(compilerOptions) !== ModuleResolutionKind.Node16 && getEmitModuleResolutionKind(compilerOptions) !== ModuleResolutionKind.NodeNext) {
40727-
return grammarErrorOnNode(declaration.assertClause, Diagnostics.Resolution_modes_are_only_supported_when_moduleResolution_is_node16_or_nodenext);
40732+
return grammarErrorOnNode(declaration.assertClause, Diagnostics.resolution_mode_assertions_are_only_supported_when_moduleResolution_is_node16_or_nodenext);
4072840733
}
4072940734
return; // Other grammar checks do not apply to type-only imports with resolution mode assertions
4073040735
}

src/compiler/diagnosticMessages.json

+6-2
Original file line numberDiff line numberDiff line change
@@ -1424,7 +1424,7 @@
14241424
"category": "Error",
14251425
"code": 1451
14261426
},
1427-
"Resolution modes are only supported when `moduleResolution` is `node16` or `nodenext`.": {
1427+
"'resolution-mode' assertions are only supported when `moduleResolution` is `node16` or `nodenext`.": {
14281428
"category": "Error",
14291429
"code": 1452
14301430
},
@@ -3451,6 +3451,10 @@
34513451
"category": "Error",
34523452
"code": 2838
34533453
},
3454+
"The type of this expression cannot be named without a 'resolution-mode' assertion, which is an unstable feature. Use nightly TypeScript to silence this error. Try updating with 'npm install -D typescript@next'.": {
3455+
"category": "Error",
3456+
"code": 2841
3457+
},
34543458

34553459
"Import declaration '{0}' is using private name '{1}'.": {
34563460
"category": "Error",
@@ -3880,7 +3884,7 @@
38803884
"category": "Error",
38813885
"code": 4124
38823886
},
3883-
"Resolution mode assertions are unstable. Use nightly TypeScript to silence this error. Try updating with 'npm install -D typescript@next'.": {
3887+
"'resolution-mode' assertions are unstable. Use nightly TypeScript to silence this error. Try updating with 'npm install -D typescript@next'.": {
38843888
"category": "Error",
38853889
"code": 4125
38863890
},

src/compiler/program.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -3116,7 +3116,7 @@ namespace ts {
31163116
setResolvedTypeReferenceDirective(file, fileName, resolvedTypeReferenceDirective);
31173117
const mode = ref.resolutionMode || file.impliedNodeFormat;
31183118
if (mode && getEmitModuleResolutionKind(options) !== ModuleResolutionKind.Node16 && getEmitModuleResolutionKind(options) !== ModuleResolutionKind.NodeNext) {
3119-
programDiagnostics.add(createDiagnosticForRange(file, ref, Diagnostics.Resolution_modes_are_only_supported_when_moduleResolution_is_node16_or_nodenext));
3119+
programDiagnostics.add(createDiagnosticForRange(file, ref, Diagnostics.resolution_mode_assertions_are_only_supported_when_moduleResolution_is_node16_or_nodenext));
31203120
}
31213121
processTypeReferenceDirective(fileName, mode, resolvedTypeReferenceDirective, { kind: FileIncludeKind.TypeReferenceDirective, file: file.path, index, });
31223122
}

src/compiler/transformers/declarations.ts

+9-2
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,8 @@ namespace ts {
7878
trackReferencedAmbientModule,
7979
trackExternalModuleSymbolOfImportTypeNode,
8080
reportNonlocalAugmentation,
81-
reportNonSerializableProperty
81+
reportNonSerializableProperty,
82+
reportImportTypeNodeResolutionModeOverride,
8283
};
8384
let errorNameNode: DeclarationName | undefined;
8485
let errorFallbackNode: Declaration | undefined;
@@ -235,6 +236,12 @@ namespace ts {
235236
}
236237
}
237238

239+
function reportImportTypeNodeResolutionModeOverride() {
240+
if (!isNightly() && (errorNameNode || errorFallbackNode)) {
241+
context.addDiagnostic(createDiagnosticForNode((errorNameNode || errorFallbackNode)!, Diagnostics.The_type_of_this_expression_cannot_be_named_without_a_resolution_mode_assertion_which_is_an_unstable_feature_Use_nightly_TypeScript_to_silence_this_error_Try_updating_with_npm_install_D_typescript_next));
242+
}
243+
}
244+
238245
function transformDeclarationsForJS(sourceFile: SourceFile, bundled?: boolean) {
239246
const oldDiag = getSymbolAccessibilityDiagnostic;
240247
getSymbolAccessibilityDiagnostic = (s) => (s.errorNode && canProduceDiagnostics(s.errorNode) ? createGetSymbolAccessibilityDiagnosticForNode(s.errorNode)(s) : ({
@@ -792,7 +799,7 @@ namespace ts {
792799
const mode = getResolutionModeOverrideForClause(assertClause);
793800
if (mode !== undefined) {
794801
if (!isNightly()) {
795-
context.addDiagnostic(createDiagnosticForNode(assertClause!, Diagnostics.Resolution_mode_assertions_are_unstable_Use_nightly_TypeScript_to_silence_this_error_Try_updating_with_npm_install_D_typescript_next));
802+
context.addDiagnostic(createDiagnosticForNode(assertClause!, Diagnostics.resolution_mode_assertions_are_unstable_Use_nightly_TypeScript_to_silence_this_error_Try_updating_with_npm_install_D_typescript_next));
796803
}
797804
return assertClause;
798805
}

src/compiler/types.ts

+1
Original file line numberDiff line numberDiff line change
@@ -8511,6 +8511,7 @@ namespace ts {
85118511
trackExternalModuleSymbolOfImportTypeNode?(symbol: Symbol): void;
85128512
reportNonlocalAugmentation?(containingFile: SourceFile, parentSymbol: Symbol, augmentingSymbol: Symbol): void;
85138513
reportNonSerializableProperty?(propertyName: string): void;
8514+
reportImportTypeNodeResolutionModeOverride?(): void;
85148515
}
85158516

85168517
export interface TextSpan {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
tests/cases/conformance/node/other.cts(3,14): error TS2841: The type of this expression cannot be named without a 'resolution-mode' assertion, which is an unstable feature. Use nightly TypeScript to silence this error. Try updating with 'npm install -D typescript@next'.
2+
tests/cases/conformance/node/other.cts(4,14): error TS2841: The type of this expression cannot be named without a 'resolution-mode' assertion, which is an unstable feature. Use nightly TypeScript to silence this error. Try updating with 'npm install -D typescript@next'.
3+
tests/cases/conformance/node/other2.cts(3,14): error TS2841: The type of this expression cannot be named without a 'resolution-mode' assertion, which is an unstable feature. Use nightly TypeScript to silence this error. Try updating with 'npm install -D typescript@next'.
4+
5+
6+
==== tests/cases/conformance/node/index.ts (0 errors) ====
7+
// esm format file
8+
export {};
9+
==== tests/cases/conformance/node/index.mts (0 errors) ====
10+
// esm format file
11+
export {};
12+
==== tests/cases/conformance/node/index.cts (0 errors) ====
13+
// cjs format file
14+
export {};
15+
==== tests/cases/conformance/node/other.ts (0 errors) ====
16+
// esm format file
17+
export const a = await import("package/cjs");
18+
export const b = await import("package/mjs");
19+
export const c = await import("package");
20+
export const f = await import("inner");
21+
==== tests/cases/conformance/node/other2.ts (0 errors) ====
22+
// esm format file
23+
export const d = await import("inner/cjs");
24+
export const e = await import("inner/mjs");
25+
==== tests/cases/conformance/node/other.mts (0 errors) ====
26+
// esm format file
27+
export const a = await import("package/cjs");
28+
export const b = await import("package/mjs");
29+
export const c = await import("package");
30+
export const f = await import("inner");
31+
==== tests/cases/conformance/node/other2.mts (0 errors) ====
32+
// esm format file
33+
export const d = await import("inner/cjs");
34+
export const e = await import("inner/mjs");
35+
==== tests/cases/conformance/node/other.cts (2 errors) ====
36+
// cjs format file, no TLA
37+
export const a = import("package/cjs");
38+
export const b = import("package/mjs");
39+
~
40+
!!! error TS2841: The type of this expression cannot be named without a 'resolution-mode' assertion, which is an unstable feature. Use nightly TypeScript to silence this error. Try updating with 'npm install -D typescript@next'.
41+
export const c = import("package");
42+
~
43+
!!! error TS2841: The type of this expression cannot be named without a 'resolution-mode' assertion, which is an unstable feature. Use nightly TypeScript to silence this error. Try updating with 'npm install -D typescript@next'.
44+
export const f = import("inner");
45+
==== tests/cases/conformance/node/other2.cts (1 errors) ====
46+
// cjs format file, no TLA
47+
export const d = import("inner/cjs");
48+
export const e = import("inner/mjs");
49+
~
50+
!!! error TS2841: The type of this expression cannot be named without a 'resolution-mode' assertion, which is an unstable feature. Use nightly TypeScript to silence this error. Try updating with 'npm install -D typescript@next'.
51+
==== tests/cases/conformance/node/node_modules/inner/index.d.ts (0 errors) ====
52+
// cjs format file
53+
export const cjsMain = true;
54+
==== tests/cases/conformance/node/node_modules/inner/index.d.mts (0 errors) ====
55+
// esm format file
56+
export const esm = true;
57+
==== tests/cases/conformance/node/node_modules/inner/index.d.cts (0 errors) ====
58+
// cjs format file
59+
export const cjsNonmain = true;
60+
==== tests/cases/conformance/node/package.json (0 errors) ====
61+
{
62+
"name": "package",
63+
"private": true,
64+
"type": "module",
65+
"exports": {
66+
"./cjs": "./index.cjs",
67+
"./mjs": "./index.mjs",
68+
".": "./index.js"
69+
}
70+
}
71+
==== tests/cases/conformance/node/node_modules/inner/package.json (0 errors) ====
72+
{
73+
"name": "inner",
74+
"private": true,
75+
"exports": {
76+
"./cjs": "./index.cjs",
77+
"./mjs": "./index.mjs",
78+
".": "./index.js"
79+
}
80+
}

tests/baselines/reference/nodeModulesDeclarationEmitDynamicImportWithPackageExports.js

-16
Original file line numberDiff line numberDiff line change
@@ -153,19 +153,3 @@ export declare const d: {
153153
cjsNonmain: true;
154154
};
155155
export declare const e: typeof import("inner/mjs");
156-
//// [other.d.cts]
157-
export declare const a: Promise<{
158-
default: typeof import("./index.cjs");
159-
}>;
160-
export declare const b: Promise<typeof import("./index.mjs", { assert: { "resolution-mode": "import" } })>;
161-
export declare const c: Promise<typeof import("./index.js", { assert: { "resolution-mode": "import" } })>;
162-
export declare const f: Promise<{
163-
default: typeof import("inner");
164-
cjsMain: true;
165-
}>;
166-
//// [other2.d.cts]
167-
export declare const d: Promise<{
168-
default: typeof import("inner/cjs");
169-
cjsNonmain: true;
170-
}>;
171-
export declare const e: Promise<typeof import("inner/mjs", { assert: { "resolution-mode": "import" } })>;

tests/baselines/reference/nodeModulesImportModeDeclarationEmit1(module=node16).errors.txt

+12-12
Original file line numberDiff line numberDiff line change
@@ -1,44 +1,44 @@
1-
/index.ts(1,45): error TS4125: Resolution mode assertions are unstable. Use nightly TypeScript to silence this error. Try updating with 'npm install -D typescript@next'.
2-
/index.ts(2,44): error TS4125: Resolution mode assertions are unstable. Use nightly TypeScript to silence this error. Try updating with 'npm install -D typescript@next'.
1+
/index.ts(1,45): error TS4125: 'resolution-mode' assertions are unstable. Use nightly TypeScript to silence this error. Try updating with 'npm install -D typescript@next'.
2+
/index.ts(2,44): error TS4125: 'resolution-mode' assertions are unstable. Use nightly TypeScript to silence this error. Try updating with 'npm install -D typescript@next'.
33
/index.ts(6,50): error TS2821: Import assertions are only supported when the '--module' option is set to 'esnext' or 'nodenext'.
4-
/index.ts(6,50): error TS4125: Resolution mode assertions are unstable. Use nightly TypeScript to silence this error. Try updating with 'npm install -D typescript@next'.
4+
/index.ts(6,50): error TS4125: 'resolution-mode' assertions are unstable. Use nightly TypeScript to silence this error. Try updating with 'npm install -D typescript@next'.
55
/index.ts(7,14): error TS2305: Module '"pkg"' has no exported member 'ImportInterface'.
66
/index.ts(7,49): error TS2821: Import assertions are only supported when the '--module' option is set to 'esnext' or 'nodenext'.
7-
/index.ts(7,49): error TS4125: Resolution mode assertions are unstable. Use nightly TypeScript to silence this error. Try updating with 'npm install -D typescript@next'.
8-
/index.ts(10,45): error TS4125: Resolution mode assertions are unstable. Use nightly TypeScript to silence this error. Try updating with 'npm install -D typescript@next'.
9-
/index.ts(11,44): error TS4125: Resolution mode assertions are unstable. Use nightly TypeScript to silence this error. Try updating with 'npm install -D typescript@next'.
7+
/index.ts(7,49): error TS4125: 'resolution-mode' assertions are unstable. Use nightly TypeScript to silence this error. Try updating with 'npm install -D typescript@next'.
8+
/index.ts(10,45): error TS4125: 'resolution-mode' assertions are unstable. Use nightly TypeScript to silence this error. Try updating with 'npm install -D typescript@next'.
9+
/index.ts(11,44): error TS4125: 'resolution-mode' assertions are unstable. Use nightly TypeScript to silence this error. Try updating with 'npm install -D typescript@next'.
1010

1111

1212
==== /index.ts (9 errors) ====
1313
import type { RequireInterface } from "pkg" assert { "resolution-mode": "require" };
1414
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
15-
!!! error TS4125: Resolution mode assertions are unstable. Use nightly TypeScript to silence this error. Try updating with 'npm install -D typescript@next'.
15+
!!! error TS4125: 'resolution-mode' assertions are unstable. Use nightly TypeScript to silence this error. Try updating with 'npm install -D typescript@next'.
1616
import type { ImportInterface } from "pkg" assert { "resolution-mode": "import" };
1717
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
18-
!!! error TS4125: Resolution mode assertions are unstable. Use nightly TypeScript to silence this error. Try updating with 'npm install -D typescript@next'.
18+
!!! error TS4125: 'resolution-mode' assertions are unstable. Use nightly TypeScript to silence this error. Try updating with 'npm install -D typescript@next'.
1919

2020
export interface LocalInterface extends RequireInterface, ImportInterface {}
2121

2222
import {type RequireInterface as Req} from "pkg" assert { "resolution-mode": "require" };
2323
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
2424
!!! error TS2821: Import assertions are only supported when the '--module' option is set to 'esnext' or 'nodenext'.
2525
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
26-
!!! error TS4125: Resolution mode assertions are unstable. Use nightly TypeScript to silence this error. Try updating with 'npm install -D typescript@next'.
26+
!!! error TS4125: 'resolution-mode' assertions are unstable. Use nightly TypeScript to silence this error. Try updating with 'npm install -D typescript@next'.
2727
import {type ImportInterface as Imp} from "pkg" assert { "resolution-mode": "import" };
2828
~~~~~~~~~~~~~~~
2929
!!! error TS2305: Module '"pkg"' has no exported member 'ImportInterface'.
3030
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
3131
!!! error TS2821: Import assertions are only supported when the '--module' option is set to 'esnext' or 'nodenext'.
3232
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
33-
!!! error TS4125: Resolution mode assertions are unstable. Use nightly TypeScript to silence this error. Try updating with 'npm install -D typescript@next'.
33+
!!! error TS4125: 'resolution-mode' assertions are unstable. Use nightly TypeScript to silence this error. Try updating with 'npm install -D typescript@next'.
3434
export interface Loc extends Req, Imp {}
3535

3636
export type { RequireInterface } from "pkg" assert { "resolution-mode": "require" };
3737
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
38-
!!! error TS4125: Resolution mode assertions are unstable. Use nightly TypeScript to silence this error. Try updating with 'npm install -D typescript@next'.
38+
!!! error TS4125: 'resolution-mode' assertions are unstable. Use nightly TypeScript to silence this error. Try updating with 'npm install -D typescript@next'.
3939
export type { ImportInterface } from "pkg" assert { "resolution-mode": "import" };
4040
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
41-
!!! error TS4125: Resolution mode assertions are unstable. Use nightly TypeScript to silence this error. Try updating with 'npm install -D typescript@next'.
41+
!!! error TS4125: 'resolution-mode' assertions are unstable. Use nightly TypeScript to silence this error. Try updating with 'npm install -D typescript@next'.
4242

4343
==== /node_modules/pkg/package.json (0 errors) ====
4444
{

0 commit comments

Comments
 (0)