Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add nightly-only error on ImportType resolution mode assertion #49356

Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 5 additions & 0 deletions src/compiler/checker.ts
Expand Up @@ -6224,6 +6224,7 @@ namespace ts {
factory.createStringLiteral("import")
)
])));
context.tracker.reportImportTypeNodeResolutionModeOverride?.();
}
}
if (!specifier) {
Expand All @@ -6247,6 +6248,7 @@ namespace ts {
factory.createStringLiteral(swappedMode === ModuleKind.ESNext ? "import" : "require")
)
])));
context.tracker.reportImportTypeNodeResolutionModeOverride?.();
}
}

Expand Down Expand Up @@ -36029,6 +36031,9 @@ namespace ts {
if (node.assertions) {
const override = getResolutionModeOverrideForClause(node.assertions.assertClause, grammarErrorOnNode);
if (override) {
if (!isNightly()) {
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);
}
if (getEmitModuleResolutionKind(compilerOptions) !== ModuleResolutionKind.Node16 && getEmitModuleResolutionKind(compilerOptions) !== ModuleResolutionKind.NodeNext) {
grammarErrorOnNode(node.assertions.assertClause, Diagnostics.Resolution_modes_are_only_supported_when_moduleResolution_is_node16_or_nodenext);
}
Expand Down
2 changes: 1 addition & 1 deletion src/compiler/corePublic.ts
Expand Up @@ -5,7 +5,7 @@ namespace ts {
// The following is baselined as a literal template type without intervention
/** The version of the TypeScript compiler release */
// eslint-disable-next-line @typescript-eslint/no-inferrable-types
export const version: string = `${versionMajorMinor}.0-dev`;
export const version: string = `${versionMajorMinor}.0`;

/**
* Type of objects whose values are all of the same type.
Expand Down
4 changes: 4 additions & 0 deletions src/compiler/diagnosticMessages.json
Expand Up @@ -3479,6 +3479,10 @@
"category": "Error",
"code": 2840
},
"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'.": {
"category": "Error",
"code": 2841
},

"Import declaration '{0}' is using private name '{1}'.": {
"category": "Error",
Expand Down
19 changes: 13 additions & 6 deletions src/compiler/transformers/declarations.ts
Expand Up @@ -78,7 +78,8 @@ namespace ts {
trackReferencedAmbientModule,
trackExternalModuleSymbolOfImportTypeNode,
reportNonlocalAugmentation,
reportNonSerializableProperty
reportNonSerializableProperty,
reportImportTypeNodeResolutionModeOverride,
};
let errorNameNode: DeclarationName | undefined;
let errorFallbackNode: Declaration | undefined;
Expand Down Expand Up @@ -235,6 +236,12 @@ namespace ts {
}
}

function reportImportTypeNodeResolutionModeOverride() {
if (!isNightly() && (errorNameNode || errorFallbackNode)) {
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));
}
}

function transformDeclarationsForJS(sourceFile: SourceFile, bundled?: boolean) {
const oldDiag = getSymbolAccessibilityDiagnostic;
getSymbolAccessibilityDiagnostic = (s) => (s.errorNode && canProduceDiagnostics(s.errorNode) ? createGetSymbolAccessibilityDiagnosticForNode(s.errorNode)(s) : ({
Expand Down Expand Up @@ -788,13 +795,13 @@ namespace ts {
// Nothing visible
}

function getResolutionModeOverrideForClauseInNightly(assertClause: AssertClause | undefined) {
const mode = getResolutionModeOverrideForClause(assertClause);
function getResolutionModeOverrideForClauseInNightly<T extends AssertClause | ImportTypeAssertionContainer>(container: T | undefined): T | undefined {
const mode = getResolutionModeOverrideForClause(container?.kind === SyntaxKind.AssertClause ? container : container?.assertClause);
if (mode !== undefined) {
if (!isNightly()) {
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));
context.addDiagnostic(createDiagnosticForNode(container!, Diagnostics.Resolution_mode_assertions_are_unstable_Use_nightly_TypeScript_to_silence_this_error_Try_updating_with_npm_install_D_typescript_next));
}
return assertClause;
return container;
}
return undefined;
}
Expand Down Expand Up @@ -1067,7 +1074,7 @@ namespace ts {
return cleanup(factory.updateImportTypeNode(
input,
factory.updateLiteralTypeNode(input.argument, rewriteModuleSpecifier(input, input.argument.literal)),
input.assertions,
getResolutionModeOverrideForClauseInNightly(input.assertions),
input.qualifier,
visitNodes(input.typeArguments, visitDeclarationSubtree, isTypeNode),
input.isTypeOf
Expand Down
1 change: 1 addition & 0 deletions src/compiler/types.ts
Expand Up @@ -8524,6 +8524,7 @@ namespace ts {
trackExternalModuleSymbolOfImportTypeNode?(symbol: Symbol): void;
reportNonlocalAugmentation?(containingFile: SourceFile, parentSymbol: Symbol, augmentingSymbol: Symbol): void;
reportNonSerializableProperty?(propertyName: string): void;
reportImportTypeNodeResolutionModeOverride?(): void;
}

export interface TextSpan {
Expand Down
@@ -0,0 +1,80 @@
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'.
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'.
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'.


==== tests/cases/conformance/node/index.ts (0 errors) ====
// esm format file
export {};
==== tests/cases/conformance/node/index.mts (0 errors) ====
// esm format file
export {};
==== tests/cases/conformance/node/index.cts (0 errors) ====
// cjs format file
export {};
==== tests/cases/conformance/node/other.ts (0 errors) ====
// esm format file
export const a = await import("package/cjs");
export const b = await import("package/mjs");
export const c = await import("package");
export const f = await import("inner");
==== tests/cases/conformance/node/other2.ts (0 errors) ====
// esm format file
export const d = await import("inner/cjs");
export const e = await import("inner/mjs");
==== tests/cases/conformance/node/other.mts (0 errors) ====
// esm format file
export const a = await import("package/cjs");
export const b = await import("package/mjs");
export const c = await import("package");
export const f = await import("inner");
==== tests/cases/conformance/node/other2.mts (0 errors) ====
// esm format file
export const d = await import("inner/cjs");
export const e = await import("inner/mjs");
==== tests/cases/conformance/node/other.cts (2 errors) ====
// cjs format file, no TLA
export const a = import("package/cjs");
export const b = import("package/mjs");
~
!!! 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'.
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here’s the new portability error in action. Bummer. 🥲

export const c = import("package");
~
!!! 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'.
export const f = import("inner");
==== tests/cases/conformance/node/other2.cts (1 errors) ====
// cjs format file, no TLA
export const d = import("inner/cjs");
export const e = import("inner/mjs");
~
!!! 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'.
==== tests/cases/conformance/node/node_modules/inner/index.d.ts (0 errors) ====
// cjs format file
export const cjsMain = true;
==== tests/cases/conformance/node/node_modules/inner/index.d.mts (0 errors) ====
// esm format file
export const esm = true;
==== tests/cases/conformance/node/node_modules/inner/index.d.cts (0 errors) ====
// cjs format file
export const cjsNonmain = true;
==== tests/cases/conformance/node/package.json (0 errors) ====
{
"name": "package",
"private": true,
"type": "module",
"exports": {
"./cjs": "./index.cjs",
"./mjs": "./index.mjs",
".": "./index.js"
}
}
==== tests/cases/conformance/node/node_modules/inner/package.json (0 errors) ====
{
"name": "inner",
"private": true,
"exports": {
"./cjs": "./index.cjs",
"./mjs": "./index.mjs",
".": "./index.js"
}
}
Expand Up @@ -153,19 +153,3 @@ export declare const d: {
cjsNonmain: true;
};
export declare const e: typeof import("inner/mjs");
//// [other.d.cts]
export declare const a: Promise<{
default: typeof import("./index.cjs");
}>;
export declare const b: Promise<typeof import("./index.mjs", { assert: { "resolution-mode": "import" } })>;
export declare const c: Promise<typeof import("./index.js", { assert: { "resolution-mode": "import" } })>;
export declare const f: Promise<{
default: typeof import("inner");
cjsMain: true;
}>;
//// [other2.d.cts]
export declare const d: Promise<{
default: typeof import("inner/cjs");
cjsNonmain: true;
}>;
export declare const e: Promise<typeof import("inner/mjs", { assert: { "resolution-mode": "import" } })>;
@@ -1,26 +1,44 @@
/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'.
/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'.
/index.ts(6,50): error TS2821: Import assertions are only supported when the '--module' option is set to 'esnext' or 'nodenext'.
/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'.
/index.ts(7,14): error TS2305: Module '"pkg"' has no exported member 'ImportInterface'.
/index.ts(7,49): error TS2821: Import assertions are only supported when the '--module' option is set to 'esnext' or 'nodenext'.
/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'.
/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'.
/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'.


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

export interface LocalInterface extends RequireInterface, ImportInterface {}

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

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

==== /node_modules/pkg/package.json (0 errors) ====
{
Expand Down
Expand Up @@ -30,16 +30,3 @@ export type { ImportInterface } from "pkg" assert { "resolution-mode": "import"
//// [index.js]
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });


//// [index.d.ts]
import type { RequireInterface } from "pkg" assert { "resolution-mode": "require" };
import type { ImportInterface } from "pkg" assert { "resolution-mode": "import" };
export interface LocalInterface extends RequireInterface, ImportInterface {
}
import { type RequireInterface as Req } from "pkg" assert { "resolution-mode": "require" };
import { type ImportInterface as Imp } from "pkg" assert { "resolution-mode": "import" };
export interface Loc extends Req, Imp {
}
export type { RequireInterface } from "pkg" assert { "resolution-mode": "require" };
export type { ImportInterface } from "pkg" assert { "resolution-mode": "import" };