Skip to content

Commit 206ed1a

Browse files
authored
Deprecate assert in import() (#63172)
1 parent e688ac8 commit 206ed1a

File tree

45 files changed

+501
-340
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

45 files changed

+501
-340
lines changed

src/compiler/checker.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37952,6 +37952,14 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
3795237952
if (importCallOptionsType !== emptyObjectType) {
3795337953
checkTypeAssignableTo(optionsType, getNullableType(importCallOptionsType, TypeFlags.Undefined), node.arguments[1]);
3795437954
}
37955+
if (compilerOptions.ignoreDeprecations !== "6.0" && isObjectLiteralExpression(node.arguments[1])) {
37956+
for (const prop of node.arguments[1].properties) {
37957+
if (isPropertyAssignment(prop) && isIdentifier(prop.name) && prop.name.escapedText === "assert") {
37958+
grammarErrorOnNode(prop.name, Diagnostics.Import_assertions_have_been_replaced_by_import_attributes_Use_with_instead_of_assert);
37959+
break;
37960+
}
37961+
}
37962+
}
3795537963
}
3795637964

3795737965
// resolveExternalModuleName will return undefined if the moduleReferenceExpression is not a string literal
@@ -43020,6 +43028,9 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
4302043028
checkSourceElement(node.argument);
4302143029

4302243030
if (node.attributes) {
43031+
if (node.attributes.token !== SyntaxKind.WithKeyword && compilerOptions.ignoreDeprecations !== "6.0") {
43032+
grammarErrorOnFirstToken(node.attributes, Diagnostics.Import_assertions_have_been_replaced_by_import_attributes_Use_with_instead_of_assert);
43033+
}
4302343034
getResolutionModeOverride(node.attributes, grammarErrorOnNode);
4302443035
}
4302543036
checkTypeReferenceOrImport(node);
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
/main.ts(1,38): error TS2880: Import assertions have been replaced by import attributes. Use 'with' instead of 'assert'.
2+
/main.ts(2,38): error TS2880: Import assertions have been replaced by import attributes. Use 'with' instead of 'assert'.
3+
/main.ts(4,31): error TS2880: Import assertions have been replaced by import attributes. Use 'with' instead of 'assert'.
4+
/main.ts(5,31): error TS2880: Import assertions have been replaced by import attributes. Use 'with' instead of 'assert'.
5+
6+
7+
==== /types.d.ts (0 errors) ====
8+
export interface MyType { x: string }
9+
10+
==== /main.ts (4 errors) ====
11+
type A = import("./types", { assert: { "resolution-mode": "import" } }).MyType;
12+
~
13+
!!! error TS2880: Import assertions have been replaced by import attributes. Use 'with' instead of 'assert'.
14+
type B = import("./types", { assert: { "resolution-mode": "require" } }).MyType;
15+
~
16+
!!! error TS2880: Import assertions have been replaced by import attributes. Use 'with' instead of 'assert'.
17+
18+
const a = import("./types", { assert: { "resolution-mode": "import" } });
19+
~~~~~~
20+
!!! error TS2880: Import assertions have been replaced by import attributes. Use 'with' instead of 'assert'.
21+
const b = import("./types", { assert: { "resolution-mode": "require" } });
22+
~~~~~~
23+
!!! error TS2880: Import assertions have been replaced by import attributes. Use 'with' instead of 'assert'.
24+

tests/baselines/reference/nodeModulesImportTypeModeDeclarationEmit1(module=node16).js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,11 @@ export interface ImportInterface {}
1515
export interface RequireInterface {}
1616
//// [index.ts]
1717
export type LocalInterface =
18-
& import("pkg", { assert: {"resolution-mode": "require"} }).RequireInterface
19-
& import("pkg", { assert: {"resolution-mode": "import"} }).ImportInterface;
18+
& import("pkg", { with: {"resolution-mode": "require"} }).RequireInterface
19+
& import("pkg", { with: {"resolution-mode": "import"} }).ImportInterface;
2020

21-
export const a = (null as any as import("pkg", { assert: {"resolution-mode": "require"} }).RequireInterface);
22-
export const b = (null as any as import("pkg", { assert: {"resolution-mode": "import"} }).ImportInterface);
21+
export const a = (null as any as import("pkg", { with: {"resolution-mode": "require"} }).RequireInterface);
22+
export const b = (null as any as import("pkg", { with: {"resolution-mode": "import"} }).ImportInterface);
2323

2424

2525
//// [index.js]
@@ -31,6 +31,6 @@ exports.b = null;
3131

3232

3333
//// [index.d.ts]
34-
export type LocalInterface = import("pkg", { assert: { "resolution-mode": "require" } }).RequireInterface & import("pkg", { assert: { "resolution-mode": "import" } }).ImportInterface;
35-
export declare const a: import("pkg").RequireInterface;
34+
export type LocalInterface = import("pkg", { with: { "resolution-mode": "require" } }).RequireInterface & import("pkg", { with: { "resolution-mode": "import" } }).ImportInterface;
35+
export declare const a: import("pkg", { with: { "resolution-mode": "require" } }).RequireInterface;
3636
export declare const b: import("pkg", { with: { "resolution-mode": "import" } }).ImportInterface;

tests/baselines/reference/nodeModulesImportTypeModeDeclarationEmit1(module=node16).symbols

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,17 @@
44
export type LocalInterface =
55
>LocalInterface : Symbol(LocalInterface, Decl(index.ts, 0, 0))
66

7-
& import("pkg", { assert: {"resolution-mode": "require"} }).RequireInterface
7+
& import("pkg", { with: {"resolution-mode": "require"} }).RequireInterface
88
>RequireInterface : Symbol(RequireInterface, Decl(require.d.ts, 0, 0))
99

10-
& import("pkg", { assert: {"resolution-mode": "import"} }).ImportInterface;
10+
& import("pkg", { with: {"resolution-mode": "import"} }).ImportInterface;
1111
>ImportInterface : Symbol(ImportInterface, Decl(import.d.ts, 0, 0))
1212

13-
export const a = (null as any as import("pkg", { assert: {"resolution-mode": "require"} }).RequireInterface);
13+
export const a = (null as any as import("pkg", { with: {"resolution-mode": "require"} }).RequireInterface);
1414
>a : Symbol(a, Decl(index.ts, 4, 12))
1515
>RequireInterface : Symbol(RequireInterface, Decl(require.d.ts, 0, 0))
1616

17-
export const b = (null as any as import("pkg", { assert: {"resolution-mode": "import"} }).ImportInterface);
17+
export const b = (null as any as import("pkg", { with: {"resolution-mode": "import"} }).ImportInterface);
1818
>b : Symbol(b, Decl(index.ts, 5, 12))
1919
>ImportInterface : Symbol(ImportInterface, Decl(import.d.ts, 0, 0))
2020

tests/baselines/reference/nodeModulesImportTypeModeDeclarationEmit1(module=node16).types

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,25 +5,25 @@ export type LocalInterface =
55
>LocalInterface : LocalInterface
66
> : ^^^^^^^^^^^^^^
77

8-
& import("pkg", { assert: {"resolution-mode": "require"} }).RequireInterface
9-
& import("pkg", { assert: {"resolution-mode": "import"} }).ImportInterface;
8+
& import("pkg", { with: {"resolution-mode": "require"} }).RequireInterface
9+
& import("pkg", { with: {"resolution-mode": "import"} }).ImportInterface;
1010

11-
export const a = (null as any as import("pkg", { assert: {"resolution-mode": "require"} }).RequireInterface);
11+
export const a = (null as any as import("pkg", { with: {"resolution-mode": "require"} }).RequireInterface);
1212
>a : import("pkg").RequireInterface
1313
> : ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
14-
>(null as any as import("pkg", { assert: {"resolution-mode": "require"} }).RequireInterface) : import("pkg").RequireInterface
15-
> : ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
16-
>null as any as import("pkg", { assert: {"resolution-mode": "require"} }).RequireInterface : import("pkg").RequireInterface
14+
>(null as any as import("pkg", { with: {"resolution-mode": "require"} }).RequireInterface) : import("pkg").RequireInterface
1715
> : ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
16+
>null as any as import("pkg", { with: {"resolution-mode": "require"} }).RequireInterface : import("pkg").RequireInterface
17+
> : ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
1818
>null as any : any
1919

20-
export const b = (null as any as import("pkg", { assert: {"resolution-mode": "import"} }).ImportInterface);
20+
export const b = (null as any as import("pkg", { with: {"resolution-mode": "import"} }).ImportInterface);
2121
>b : import("./node_modules/pkg/import").ImportInterface
2222
> : ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
23-
>(null as any as import("pkg", { assert: {"resolution-mode": "import"} }).ImportInterface) : import("./node_modules/pkg/import").ImportInterface
24-
> : ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
25-
>null as any as import("pkg", { assert: {"resolution-mode": "import"} }).ImportInterface : import("./node_modules/pkg/import").ImportInterface
23+
>(null as any as import("pkg", { with: {"resolution-mode": "import"} }).ImportInterface) : import("./node_modules/pkg/import").ImportInterface
2624
> : ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
25+
>null as any as import("pkg", { with: {"resolution-mode": "import"} }).ImportInterface : import("./node_modules/pkg/import").ImportInterface
26+
> : ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
2727
>null as any : any
2828

2929
=== /node_modules/pkg/import.d.ts ===

tests/baselines/reference/nodeModulesImportTypeModeDeclarationEmit1(module=node18).js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,11 @@ export interface ImportInterface {}
1515
export interface RequireInterface {}
1616
//// [index.ts]
1717
export type LocalInterface =
18-
& import("pkg", { assert: {"resolution-mode": "require"} }).RequireInterface
19-
& import("pkg", { assert: {"resolution-mode": "import"} }).ImportInterface;
18+
& import("pkg", { with: {"resolution-mode": "require"} }).RequireInterface
19+
& import("pkg", { with: {"resolution-mode": "import"} }).ImportInterface;
2020

21-
export const a = (null as any as import("pkg", { assert: {"resolution-mode": "require"} }).RequireInterface);
22-
export const b = (null as any as import("pkg", { assert: {"resolution-mode": "import"} }).ImportInterface);
21+
export const a = (null as any as import("pkg", { with: {"resolution-mode": "require"} }).RequireInterface);
22+
export const b = (null as any as import("pkg", { with: {"resolution-mode": "import"} }).ImportInterface);
2323

2424

2525
//// [index.js]
@@ -31,6 +31,6 @@ exports.b = null;
3131

3232

3333
//// [index.d.ts]
34-
export type LocalInterface = import("pkg", { assert: { "resolution-mode": "require" } }).RequireInterface & import("pkg", { assert: { "resolution-mode": "import" } }).ImportInterface;
35-
export declare const a: import("pkg").RequireInterface;
34+
export type LocalInterface = import("pkg", { with: { "resolution-mode": "require" } }).RequireInterface & import("pkg", { with: { "resolution-mode": "import" } }).ImportInterface;
35+
export declare const a: import("pkg", { with: { "resolution-mode": "require" } }).RequireInterface;
3636
export declare const b: import("pkg", { with: { "resolution-mode": "import" } }).ImportInterface;

tests/baselines/reference/nodeModulesImportTypeModeDeclarationEmit1(module=node18).symbols

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,17 @@
44
export type LocalInterface =
55
>LocalInterface : Symbol(LocalInterface, Decl(index.ts, 0, 0))
66

7-
& import("pkg", { assert: {"resolution-mode": "require"} }).RequireInterface
7+
& import("pkg", { with: {"resolution-mode": "require"} }).RequireInterface
88
>RequireInterface : Symbol(RequireInterface, Decl(require.d.ts, 0, 0))
99

10-
& import("pkg", { assert: {"resolution-mode": "import"} }).ImportInterface;
10+
& import("pkg", { with: {"resolution-mode": "import"} }).ImportInterface;
1111
>ImportInterface : Symbol(ImportInterface, Decl(import.d.ts, 0, 0))
1212

13-
export const a = (null as any as import("pkg", { assert: {"resolution-mode": "require"} }).RequireInterface);
13+
export const a = (null as any as import("pkg", { with: {"resolution-mode": "require"} }).RequireInterface);
1414
>a : Symbol(a, Decl(index.ts, 4, 12))
1515
>RequireInterface : Symbol(RequireInterface, Decl(require.d.ts, 0, 0))
1616

17-
export const b = (null as any as import("pkg", { assert: {"resolution-mode": "import"} }).ImportInterface);
17+
export const b = (null as any as import("pkg", { with: {"resolution-mode": "import"} }).ImportInterface);
1818
>b : Symbol(b, Decl(index.ts, 5, 12))
1919
>ImportInterface : Symbol(ImportInterface, Decl(import.d.ts, 0, 0))
2020

tests/baselines/reference/nodeModulesImportTypeModeDeclarationEmit1(module=node18).types

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,25 +5,25 @@ export type LocalInterface =
55
>LocalInterface : LocalInterface
66
> : ^^^^^^^^^^^^^^
77

8-
& import("pkg", { assert: {"resolution-mode": "require"} }).RequireInterface
9-
& import("pkg", { assert: {"resolution-mode": "import"} }).ImportInterface;
8+
& import("pkg", { with: {"resolution-mode": "require"} }).RequireInterface
9+
& import("pkg", { with: {"resolution-mode": "import"} }).ImportInterface;
1010

11-
export const a = (null as any as import("pkg", { assert: {"resolution-mode": "require"} }).RequireInterface);
11+
export const a = (null as any as import("pkg", { with: {"resolution-mode": "require"} }).RequireInterface);
1212
>a : import("pkg").RequireInterface
1313
> : ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
14-
>(null as any as import("pkg", { assert: {"resolution-mode": "require"} }).RequireInterface) : import("pkg").RequireInterface
15-
> : ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
16-
>null as any as import("pkg", { assert: {"resolution-mode": "require"} }).RequireInterface : import("pkg").RequireInterface
14+
>(null as any as import("pkg", { with: {"resolution-mode": "require"} }).RequireInterface) : import("pkg").RequireInterface
1715
> : ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
16+
>null as any as import("pkg", { with: {"resolution-mode": "require"} }).RequireInterface : import("pkg").RequireInterface
17+
> : ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
1818
>null as any : any
1919

20-
export const b = (null as any as import("pkg", { assert: {"resolution-mode": "import"} }).ImportInterface);
20+
export const b = (null as any as import("pkg", { with: {"resolution-mode": "import"} }).ImportInterface);
2121
>b : import("./node_modules/pkg/import").ImportInterface
2222
> : ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
23-
>(null as any as import("pkg", { assert: {"resolution-mode": "import"} }).ImportInterface) : import("./node_modules/pkg/import").ImportInterface
24-
> : ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
25-
>null as any as import("pkg", { assert: {"resolution-mode": "import"} }).ImportInterface : import("./node_modules/pkg/import").ImportInterface
23+
>(null as any as import("pkg", { with: {"resolution-mode": "import"} }).ImportInterface) : import("./node_modules/pkg/import").ImportInterface
2624
> : ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
25+
>null as any as import("pkg", { with: {"resolution-mode": "import"} }).ImportInterface : import("./node_modules/pkg/import").ImportInterface
26+
> : ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
2727
>null as any : any
2828

2929
=== /node_modules/pkg/import.d.ts ===

tests/baselines/reference/nodeModulesImportTypeModeDeclarationEmit1(module=node20).js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,11 @@ export interface ImportInterface {}
1515
export interface RequireInterface {}
1616
//// [index.ts]
1717
export type LocalInterface =
18-
& import("pkg", { assert: {"resolution-mode": "require"} }).RequireInterface
19-
& import("pkg", { assert: {"resolution-mode": "import"} }).ImportInterface;
18+
& import("pkg", { with: {"resolution-mode": "require"} }).RequireInterface
19+
& import("pkg", { with: {"resolution-mode": "import"} }).ImportInterface;
2020

21-
export const a = (null as any as import("pkg", { assert: {"resolution-mode": "require"} }).RequireInterface);
22-
export const b = (null as any as import("pkg", { assert: {"resolution-mode": "import"} }).ImportInterface);
21+
export const a = (null as any as import("pkg", { with: {"resolution-mode": "require"} }).RequireInterface);
22+
export const b = (null as any as import("pkg", { with: {"resolution-mode": "import"} }).ImportInterface);
2323

2424

2525
//// [index.js]
@@ -31,6 +31,6 @@ exports.b = null;
3131

3232

3333
//// [index.d.ts]
34-
export type LocalInterface = import("pkg", { assert: { "resolution-mode": "require" } }).RequireInterface & import("pkg", { assert: { "resolution-mode": "import" } }).ImportInterface;
35-
export declare const a: import("pkg").RequireInterface;
34+
export type LocalInterface = import("pkg", { with: { "resolution-mode": "require" } }).RequireInterface & import("pkg", { with: { "resolution-mode": "import" } }).ImportInterface;
35+
export declare const a: import("pkg", { with: { "resolution-mode": "require" } }).RequireInterface;
3636
export declare const b: import("pkg", { with: { "resolution-mode": "import" } }).ImportInterface;

tests/baselines/reference/nodeModulesImportTypeModeDeclarationEmit1(module=node20).symbols

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,17 @@
44
export type LocalInterface =
55
>LocalInterface : Symbol(LocalInterface, Decl(index.ts, 0, 0))
66

7-
& import("pkg", { assert: {"resolution-mode": "require"} }).RequireInterface
7+
& import("pkg", { with: {"resolution-mode": "require"} }).RequireInterface
88
>RequireInterface : Symbol(RequireInterface, Decl(require.d.ts, 0, 0))
99

10-
& import("pkg", { assert: {"resolution-mode": "import"} }).ImportInterface;
10+
& import("pkg", { with: {"resolution-mode": "import"} }).ImportInterface;
1111
>ImportInterface : Symbol(ImportInterface, Decl(import.d.ts, 0, 0))
1212

13-
export const a = (null as any as import("pkg", { assert: {"resolution-mode": "require"} }).RequireInterface);
13+
export const a = (null as any as import("pkg", { with: {"resolution-mode": "require"} }).RequireInterface);
1414
>a : Symbol(a, Decl(index.ts, 4, 12))
1515
>RequireInterface : Symbol(RequireInterface, Decl(require.d.ts, 0, 0))
1616

17-
export const b = (null as any as import("pkg", { assert: {"resolution-mode": "import"} }).ImportInterface);
17+
export const b = (null as any as import("pkg", { with: {"resolution-mode": "import"} }).ImportInterface);
1818
>b : Symbol(b, Decl(index.ts, 5, 12))
1919
>ImportInterface : Symbol(ImportInterface, Decl(import.d.ts, 0, 0))
2020

0 commit comments

Comments
 (0)