diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index 48bc0da113816..7bf615735312c 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -48564,6 +48564,15 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker { } function checkImportAttribute(node: ImportAttribute) { + // Validate that the value is a string literal before type checking + if (!isStringLiteral(node.value)) { + const parent = node.parent; + const diagnostic = parent.token === SyntaxKind.WithKeyword + ? Diagnostics.Import_attribute_values_must_be_string_literal_expressions + : Diagnostics.Import_assertion_values_must_be_string_literal_expressions; + error(node.value, diagnostic); + return errorType; + } return getRegularTypeOfLiteralType(checkExpressionCached(node.value)); } diff --git a/tests/baselines/reference/importAssertionNonstring.errors.txt b/tests/baselines/reference/importAssertionNonstring.errors.txt index a220b1f213eaa..2112d47fd934a 100644 --- a/tests/baselines/reference/importAssertionNonstring.errors.txt +++ b/tests/baselines/reference/importAssertionNonstring.errors.txt @@ -1,37 +1,21 @@ mod.mts(1,37): error TS2880: Import assertions have been replaced by import attributes. Use 'with' instead of 'assert'. -mod.mts(1,37): error TS2322: Type '{ field: 0; }' is not assignable to type 'ImportAttributes'. - Property 'field' is incompatible with index signature. - Type 'number' is not assignable to type 'string'. mod.mts(1,52): error TS2837: Import assertion values must be string literal expressions. mod.mts(3,37): error TS2880: Import assertions have been replaced by import attributes. Use 'with' instead of 'assert'. mod.mts(3,52): error TS2837: Import assertion values must be string literal expressions. mod.mts(5,37): error TS2880: Import assertions have been replaced by import attributes. Use 'with' instead of 'assert'. -mod.mts(5,37): error TS2322: Type '{ field: RegExp; }' is not assignable to type 'ImportAttributes'. - Property 'field' is incompatible with index signature. - Type 'RegExp' is not assignable to type 'string'. mod.mts(5,52): error TS2837: Import assertion values must be string literal expressions. mod.mts(7,37): error TS2880: Import assertions have been replaced by import attributes. Use 'with' instead of 'assert'. -mod.mts(7,37): error TS2322: Type '{ field: string[]; }' is not assignable to type 'ImportAttributes'. - Property 'field' is incompatible with index signature. - Type 'string[]' is not assignable to type 'string'. mod.mts(7,52): error TS2837: Import assertion values must be string literal expressions. mod.mts(9,37): error TS2880: Import assertions have been replaced by import attributes. Use 'with' instead of 'assert'. -mod.mts(9,37): error TS2322: Type '{ field: { a: number; }; }' is not assignable to type 'ImportAttributes'. - Property 'field' is incompatible with index signature. - Type '{ a: number; }' is not assignable to type 'string'. mod.mts(9,52): error TS2837: Import assertion values must be string literal expressions. mod.mts(11,37): error TS2880: Import assertions have been replaced by import attributes. Use 'with' instead of 'assert'. mod.mts(11,66): error TS2837: Import assertion values must be string literal expressions. -==== mod.mts (16 errors) ==== +==== mod.mts (12 errors) ==== import * as thing1 from "./mod.mjs" assert {field: 0}; ~~~~~~ !!! error TS2880: Import assertions have been replaced by import attributes. Use 'with' instead of 'assert'. - ~~~~~~~~~~~~~~~~~ -!!! error TS2322: Type '{ field: 0; }' is not assignable to type 'ImportAttributes'. -!!! error TS2322: Property 'field' is incompatible with index signature. -!!! error TS2322: Type 'number' is not assignable to type 'string'. ~ !!! error TS2837: Import assertion values must be string literal expressions. @@ -44,30 +28,18 @@ mod.mts(11,66): error TS2837: Import assertion values must be string literal exp import * as thing3 from "./mod.mjs" assert {field: /a/g}; ~~~~~~ !!! error TS2880: Import assertions have been replaced by import attributes. Use 'with' instead of 'assert'. - ~~~~~~~~~~~~~~~~~~~~ -!!! error TS2322: Type '{ field: RegExp; }' is not assignable to type 'ImportAttributes'. -!!! error TS2322: Property 'field' is incompatible with index signature. -!!! error TS2322: Type 'RegExp' is not assignable to type 'string'. ~~~~ !!! error TS2837: Import assertion values must be string literal expressions. import * as thing4 from "./mod.mjs" assert {field: ["a"]}; ~~~~~~ !!! error TS2880: Import assertions have been replaced by import attributes. Use 'with' instead of 'assert'. - ~~~~~~~~~~~~~~~~~~~~~ -!!! error TS2322: Type '{ field: string[]; }' is not assignable to type 'ImportAttributes'. -!!! error TS2322: Property 'field' is incompatible with index signature. -!!! error TS2322: Type 'string[]' is not assignable to type 'string'. ~~~~~ !!! error TS2837: Import assertion values must be string literal expressions. import * as thing5 from "./mod.mjs" assert {field: { a: 0 }}; ~~~~~~ !!! error TS2880: Import assertions have been replaced by import attributes. Use 'with' instead of 'assert'. - ~~~~~~~~~~~~~~~~~~~~~~~~ -!!! error TS2322: Type '{ field: { a: number; }; }' is not assignable to type 'ImportAttributes'. -!!! error TS2322: Property 'field' is incompatible with index signature. -!!! error TS2322: Type '{ a: number; }' is not assignable to type 'string'. ~~~~~~~~ !!! error TS2837: Import assertion values must be string literal expressions. diff --git a/tests/baselines/reference/importAttributes6(module=node18).errors.txt b/tests/baselines/reference/importAttributes6(module=node18).errors.txt index f2f160a4bd0e1..18fa0ba6b693d 100644 --- a/tests/baselines/reference/importAttributes6(module=node18).errors.txt +++ b/tests/baselines/reference/importAttributes6(module=node18).errors.txt @@ -1,53 +1,25 @@ -mod.mts(1,37): error TS2322: Type '{ field: 0; }' is not assignable to type 'ImportAttributes'. - Property 'field' is incompatible with index signature. - Type 'number' is not assignable to type 'string'. mod.mts(1,51): error TS2858: Import attribute values must be string literal expressions. mod.mts(2,51): error TS2858: Import attribute values must be string literal expressions. -mod.mts(3,37): error TS2322: Type '{ field: RegExp; }' is not assignable to type 'ImportAttributes'. - Property 'field' is incompatible with index signature. - Type 'RegExp' is not assignable to type 'string'. mod.mts(3,51): error TS2858: Import attribute values must be string literal expressions. -mod.mts(4,37): error TS2322: Type '{ field: string[]; }' is not assignable to type 'ImportAttributes'. - Property 'field' is incompatible with index signature. - Type 'string[]' is not assignable to type 'string'. mod.mts(4,51): error TS2858: Import attribute values must be string literal expressions. -mod.mts(5,37): error TS2322: Type '{ field: { a: number; }; }' is not assignable to type 'ImportAttributes'. - Property 'field' is incompatible with index signature. - Type '{ a: number; }' is not assignable to type 'string'. mod.mts(5,51): error TS2858: Import attribute values must be string literal expressions. mod.mts(6,65): error TS2858: Import attribute values must be string literal expressions. -==== mod.mts (10 errors) ==== +==== mod.mts (6 errors) ==== import * as thing1 from "./mod.mjs" with { field: 0 }; - ~~~~~~~~~~~~~~~~~ -!!! error TS2322: Type '{ field: 0; }' is not assignable to type 'ImportAttributes'. -!!! error TS2322: Property 'field' is incompatible with index signature. -!!! error TS2322: Type 'number' is not assignable to type 'string'. ~ !!! error TS2858: Import attribute values must be string literal expressions. import * as thing2 from "./mod.mjs" with { field: `a` }; ~~~ !!! error TS2858: Import attribute values must be string literal expressions. import * as thing3 from "./mod.mjs" with { field: /a/g }; - ~~~~~~~~~~~~~~~~~~~~ -!!! error TS2322: Type '{ field: RegExp; }' is not assignable to type 'ImportAttributes'. -!!! error TS2322: Property 'field' is incompatible with index signature. -!!! error TS2322: Type 'RegExp' is not assignable to type 'string'. ~~~~ !!! error TS2858: Import attribute values must be string literal expressions. import * as thing4 from "./mod.mjs" with { field: ["a"] }; - ~~~~~~~~~~~~~~~~~~~~~ -!!! error TS2322: Type '{ field: string[]; }' is not assignable to type 'ImportAttributes'. -!!! error TS2322: Property 'field' is incompatible with index signature. -!!! error TS2322: Type 'string[]' is not assignable to type 'string'. ~~~~~ !!! error TS2858: Import attribute values must be string literal expressions. import * as thing5 from "./mod.mjs" with { field: { a: 0 } }; - ~~~~~~~~~~~~~~~~~~~~~~~~ -!!! error TS2322: Type '{ field: { a: number; }; }' is not assignable to type 'ImportAttributes'. -!!! error TS2322: Property 'field' is incompatible with index signature. -!!! error TS2322: Type '{ a: number; }' is not assignable to type 'string'. ~~~~~~~~ !!! error TS2858: Import attribute values must be string literal expressions. import * as thing6 from "./mod.mjs" with { type: "json", field: 0..toString() }; diff --git a/tests/baselines/reference/importAttributes6(module=node20).errors.txt b/tests/baselines/reference/importAttributes6(module=node20).errors.txt index f2f160a4bd0e1..18fa0ba6b693d 100644 --- a/tests/baselines/reference/importAttributes6(module=node20).errors.txt +++ b/tests/baselines/reference/importAttributes6(module=node20).errors.txt @@ -1,53 +1,25 @@ -mod.mts(1,37): error TS2322: Type '{ field: 0; }' is not assignable to type 'ImportAttributes'. - Property 'field' is incompatible with index signature. - Type 'number' is not assignable to type 'string'. mod.mts(1,51): error TS2858: Import attribute values must be string literal expressions. mod.mts(2,51): error TS2858: Import attribute values must be string literal expressions. -mod.mts(3,37): error TS2322: Type '{ field: RegExp; }' is not assignable to type 'ImportAttributes'. - Property 'field' is incompatible with index signature. - Type 'RegExp' is not assignable to type 'string'. mod.mts(3,51): error TS2858: Import attribute values must be string literal expressions. -mod.mts(4,37): error TS2322: Type '{ field: string[]; }' is not assignable to type 'ImportAttributes'. - Property 'field' is incompatible with index signature. - Type 'string[]' is not assignable to type 'string'. mod.mts(4,51): error TS2858: Import attribute values must be string literal expressions. -mod.mts(5,37): error TS2322: Type '{ field: { a: number; }; }' is not assignable to type 'ImportAttributes'. - Property 'field' is incompatible with index signature. - Type '{ a: number; }' is not assignable to type 'string'. mod.mts(5,51): error TS2858: Import attribute values must be string literal expressions. mod.mts(6,65): error TS2858: Import attribute values must be string literal expressions. -==== mod.mts (10 errors) ==== +==== mod.mts (6 errors) ==== import * as thing1 from "./mod.mjs" with { field: 0 }; - ~~~~~~~~~~~~~~~~~ -!!! error TS2322: Type '{ field: 0; }' is not assignable to type 'ImportAttributes'. -!!! error TS2322: Property 'field' is incompatible with index signature. -!!! error TS2322: Type 'number' is not assignable to type 'string'. ~ !!! error TS2858: Import attribute values must be string literal expressions. import * as thing2 from "./mod.mjs" with { field: `a` }; ~~~ !!! error TS2858: Import attribute values must be string literal expressions. import * as thing3 from "./mod.mjs" with { field: /a/g }; - ~~~~~~~~~~~~~~~~~~~~ -!!! error TS2322: Type '{ field: RegExp; }' is not assignable to type 'ImportAttributes'. -!!! error TS2322: Property 'field' is incompatible with index signature. -!!! error TS2322: Type 'RegExp' is not assignable to type 'string'. ~~~~ !!! error TS2858: Import attribute values must be string literal expressions. import * as thing4 from "./mod.mjs" with { field: ["a"] }; - ~~~~~~~~~~~~~~~~~~~~~ -!!! error TS2322: Type '{ field: string[]; }' is not assignable to type 'ImportAttributes'. -!!! error TS2322: Property 'field' is incompatible with index signature. -!!! error TS2322: Type 'string[]' is not assignable to type 'string'. ~~~~~ !!! error TS2858: Import attribute values must be string literal expressions. import * as thing5 from "./mod.mjs" with { field: { a: 0 } }; - ~~~~~~~~~~~~~~~~~~~~~~~~ -!!! error TS2322: Type '{ field: { a: number; }; }' is not assignable to type 'ImportAttributes'. -!!! error TS2322: Property 'field' is incompatible with index signature. -!!! error TS2322: Type '{ a: number; }' is not assignable to type 'string'. ~~~~~~~~ !!! error TS2858: Import attribute values must be string literal expressions. import * as thing6 from "./mod.mjs" with { type: "json", field: 0..toString() }; diff --git a/tests/baselines/reference/importAttributes6(module=nodenext).errors.txt b/tests/baselines/reference/importAttributes6(module=nodenext).errors.txt index f2f160a4bd0e1..18fa0ba6b693d 100644 --- a/tests/baselines/reference/importAttributes6(module=nodenext).errors.txt +++ b/tests/baselines/reference/importAttributes6(module=nodenext).errors.txt @@ -1,53 +1,25 @@ -mod.mts(1,37): error TS2322: Type '{ field: 0; }' is not assignable to type 'ImportAttributes'. - Property 'field' is incompatible with index signature. - Type 'number' is not assignable to type 'string'. mod.mts(1,51): error TS2858: Import attribute values must be string literal expressions. mod.mts(2,51): error TS2858: Import attribute values must be string literal expressions. -mod.mts(3,37): error TS2322: Type '{ field: RegExp; }' is not assignable to type 'ImportAttributes'. - Property 'field' is incompatible with index signature. - Type 'RegExp' is not assignable to type 'string'. mod.mts(3,51): error TS2858: Import attribute values must be string literal expressions. -mod.mts(4,37): error TS2322: Type '{ field: string[]; }' is not assignable to type 'ImportAttributes'. - Property 'field' is incompatible with index signature. - Type 'string[]' is not assignable to type 'string'. mod.mts(4,51): error TS2858: Import attribute values must be string literal expressions. -mod.mts(5,37): error TS2322: Type '{ field: { a: number; }; }' is not assignable to type 'ImportAttributes'. - Property 'field' is incompatible with index signature. - Type '{ a: number; }' is not assignable to type 'string'. mod.mts(5,51): error TS2858: Import attribute values must be string literal expressions. mod.mts(6,65): error TS2858: Import attribute values must be string literal expressions. -==== mod.mts (10 errors) ==== +==== mod.mts (6 errors) ==== import * as thing1 from "./mod.mjs" with { field: 0 }; - ~~~~~~~~~~~~~~~~~ -!!! error TS2322: Type '{ field: 0; }' is not assignable to type 'ImportAttributes'. -!!! error TS2322: Property 'field' is incompatible with index signature. -!!! error TS2322: Type 'number' is not assignable to type 'string'. ~ !!! error TS2858: Import attribute values must be string literal expressions. import * as thing2 from "./mod.mjs" with { field: `a` }; ~~~ !!! error TS2858: Import attribute values must be string literal expressions. import * as thing3 from "./mod.mjs" with { field: /a/g }; - ~~~~~~~~~~~~~~~~~~~~ -!!! error TS2322: Type '{ field: RegExp; }' is not assignable to type 'ImportAttributes'. -!!! error TS2322: Property 'field' is incompatible with index signature. -!!! error TS2322: Type 'RegExp' is not assignable to type 'string'. ~~~~ !!! error TS2858: Import attribute values must be string literal expressions. import * as thing4 from "./mod.mjs" with { field: ["a"] }; - ~~~~~~~~~~~~~~~~~~~~~ -!!! error TS2322: Type '{ field: string[]; }' is not assignable to type 'ImportAttributes'. -!!! error TS2322: Property 'field' is incompatible with index signature. -!!! error TS2322: Type 'string[]' is not assignable to type 'string'. ~~~~~ !!! error TS2858: Import attribute values must be string literal expressions. import * as thing5 from "./mod.mjs" with { field: { a: 0 } }; - ~~~~~~~~~~~~~~~~~~~~~~~~ -!!! error TS2322: Type '{ field: { a: number; }; }' is not assignable to type 'ImportAttributes'. -!!! error TS2322: Property 'field' is incompatible with index signature. -!!! error TS2322: Type '{ a: number; }' is not assignable to type 'string'. ~~~~~~~~ !!! error TS2858: Import attribute values must be string literal expressions. import * as thing6 from "./mod.mjs" with { type: "json", field: 0..toString() }; diff --git a/tests/baselines/reference/importTypeAttributesNonString.js b/tests/baselines/reference/importTypeAttributesNonString.js new file mode 100644 index 0000000000000..590497bcee25e --- /dev/null +++ b/tests/baselines/reference/importTypeAttributesNonString.js @@ -0,0 +1,65 @@ +//// [tests/cases/compiler/importTypeAttributesNonString.ts] //// + +//// [wat.mts] +export const x = 1; + +//// [mod.mts] +// Import type with function expression (should error) +type T1 = typeof import("./wat.mjs", { + assert: { + a: (() => { + console.log("why can you write js here????"); + })(), + } +}); + +// Import type with number literal (should error) +type T2 = typeof import("./wat.mjs", { + assert: { field: 0 } +}); + +// Import type with template literal (should error) +type T3 = typeof import("./wat.mjs", { + assert: { field: `a` } +}); + +// Import type with regex (should error) +type T4 = typeof import("./wat.mjs", { + assert: { field: /a/g } +}); + +// Import type with array (should error) +type T5 = typeof import("./wat.mjs", { + assert: { field: ["a"] } +}); + +// Import type with object (should error) +type T6 = typeof import("./wat.mjs", { + assert: { field: { a: 0 } } +}); + +// Import type with method call (should error) +type T7 = typeof import("./wat.mjs", { + assert: { field: 0..toString() } +}); + +// Import type with valid string literal (should work) +type T8 = typeof import("./wat.mjs", { + assert: { type: "json" } +}); + +// Import type with 'with' keyword and valid string (should work) +type T9 = typeof import("./wat.mjs", { + with: { type: "json" } +}); + +// Import type with 'with' keyword and invalid expression (should error) +type T10 = typeof import("./wat.mjs", { + with: { field: 123 } +}); + + +//// [wat.mjs] +export const x = 1; +//// [mod.mjs] +export {}; diff --git a/tests/baselines/reference/importTypeAttributesNonString.symbols b/tests/baselines/reference/importTypeAttributesNonString.symbols new file mode 100644 index 0000000000000..409b19a529c2d --- /dev/null +++ b/tests/baselines/reference/importTypeAttributesNonString.symbols @@ -0,0 +1,90 @@ +//// [tests/cases/compiler/importTypeAttributesNonString.ts] //// + +=== wat.mts === +export const x = 1; +>x : Symbol(x, Decl(wat.mts, 0, 12)) + +=== mod.mts === +// Import type with function expression (should error) +type T1 = typeof import("./wat.mjs", { +>T1 : Symbol(T1, Decl(mod.mts, 0, 0)) + + assert: { + a: (() => { + console.log("why can you write js here????"); +>console.log : Symbol(Console.log, Decl(lib.dom.d.ts, --, --)) +>console : Symbol(console, Decl(lib.dom.d.ts, --, --)) +>log : Symbol(Console.log, Decl(lib.dom.d.ts, --, --)) + + })(), + } +}); + +// Import type with number literal (should error) +type T2 = typeof import("./wat.mjs", { +>T2 : Symbol(T2, Decl(mod.mts, 7, 3)) + + assert: { field: 0 } +}); + +// Import type with template literal (should error) +type T3 = typeof import("./wat.mjs", { +>T3 : Symbol(T3, Decl(mod.mts, 12, 3)) + + assert: { field: `a` } +}); + +// Import type with regex (should error) +type T4 = typeof import("./wat.mjs", { +>T4 : Symbol(T4, Decl(mod.mts, 17, 3)) + + assert: { field: /a/g } +}); + +// Import type with array (should error) +type T5 = typeof import("./wat.mjs", { +>T5 : Symbol(T5, Decl(mod.mts, 22, 3)) + + assert: { field: ["a"] } +}); + +// Import type with object (should error) +type T6 = typeof import("./wat.mjs", { +>T6 : Symbol(T6, Decl(mod.mts, 27, 3)) + + assert: { field: { a: 0 } } +>a : Symbol(a, Decl(mod.mts, 31, 20)) + +}); + +// Import type with method call (should error) +type T7 = typeof import("./wat.mjs", { +>T7 : Symbol(T7, Decl(mod.mts, 32, 3)) + + assert: { field: 0..toString() } +>0..toString : Symbol(Number.toString, Decl(lib.es5.d.ts, --, --)) +>toString : Symbol(Number.toString, Decl(lib.es5.d.ts, --, --)) + +}); + +// Import type with valid string literal (should work) +type T8 = typeof import("./wat.mjs", { +>T8 : Symbol(T8, Decl(mod.mts, 37, 3)) + + assert: { type: "json" } +}); + +// Import type with 'with' keyword and valid string (should work) +type T9 = typeof import("./wat.mjs", { +>T9 : Symbol(T9, Decl(mod.mts, 42, 3)) + + with: { type: "json" } +}); + +// Import type with 'with' keyword and invalid expression (should error) +type T10 = typeof import("./wat.mjs", { +>T10 : Symbol(T10, Decl(mod.mts, 47, 3)) + + with: { field: 123 } +}); + diff --git a/tests/baselines/reference/importTypeAttributesNonString.types b/tests/baselines/reference/importTypeAttributesNonString.types new file mode 100644 index 0000000000000..28ab29153f16f --- /dev/null +++ b/tests/baselines/reference/importTypeAttributesNonString.types @@ -0,0 +1,151 @@ +//// [tests/cases/compiler/importTypeAttributesNonString.ts] //// + +=== wat.mts === +export const x = 1; +>x : 1 +> : ^ +>1 : 1 +> : ^ + +=== mod.mts === +// Import type with function expression (should error) +type T1 = typeof import("./wat.mjs", { +>T1 : typeof import("wat") +> : ^^^^^^^^^^^^^^^^^^^^ + + assert: { + a: (() => { +>a : error +>(() => { console.log("why can you write js here????"); })() : void +> : ^^^^ +>(() => { console.log("why can you write js here????"); }) : () => void +> : ^^^^^^^^^^ +>() => { console.log("why can you write js here????"); } : () => void +> : ^^^^^^^^^^ + + console.log("why can you write js here????"); +>console.log("why can you write js here????") : void +> : ^^^^ +>console.log : (...data: any[]) => void +> : ^^^^ ^^ ^^^^^ +>console : Console +> : ^^^^^^^ +>log : (...data: any[]) => void +> : ^^^^ ^^ ^^^^^ +>"why can you write js here????" : "why can you write js here????" +> : ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + + })(), + } +}); + +// Import type with number literal (should error) +type T2 = typeof import("./wat.mjs", { +>T2 : typeof import("wat") +> : ^^^^^^^^^^^^^^^^^^^^ + + assert: { field: 0 } +>field : error + +}); + +// Import type with template literal (should error) +type T3 = typeof import("./wat.mjs", { +>T3 : typeof import("wat") +> : ^^^^^^^^^^^^^^^^^^^^ + + assert: { field: `a` } +>field : error + +}); + +// Import type with regex (should error) +type T4 = typeof import("./wat.mjs", { +>T4 : typeof import("wat") +> : ^^^^^^^^^^^^^^^^^^^^ + + assert: { field: /a/g } +>field : error +>/a/g : RegExp +> : ^^^^^^ + +}); + +// Import type with array (should error) +type T5 = typeof import("./wat.mjs", { +>T5 : typeof import("wat") +> : ^^^^^^^^^^^^^^^^^^^^ + + assert: { field: ["a"] } +>field : error +>["a"] : string[] +> : ^^^^^^^^ +>"a" : "a" +> : ^^^ + +}); + +// Import type with object (should error) +type T6 = typeof import("./wat.mjs", { +>T6 : typeof import("wat") +> : ^^^^^^^^^^^^^^^^^^^^ + + assert: { field: { a: 0 } } +>field : error +>{ a: 0 } : { a: number; } +> : ^^^^^^^^^^^^^^ +>a : number +> : ^^^^^^ +>0 : 0 +> : ^ + +}); + +// Import type with method call (should error) +type T7 = typeof import("./wat.mjs", { +>T7 : typeof import("wat") +> : ^^^^^^^^^^^^^^^^^^^^ + + assert: { field: 0..toString() } +>field : error +>0..toString() : string +> : ^^^^^^ +>0..toString : (radix?: number) => string +> : ^ ^^^ ^^^^^ +>0. : 0 +> : ^ +>toString : (radix?: number) => string +> : ^ ^^^ ^^^^^ + +}); + +// Import type with valid string literal (should work) +type T8 = typeof import("./wat.mjs", { +>T8 : typeof import("wat") +> : ^^^^^^^^^^^^^^^^^^^^ + + assert: { type: "json" } +>type : error + +}); + +// Import type with 'with' keyword and valid string (should work) +type T9 = typeof import("./wat.mjs", { +>T9 : typeof import("wat") +> : ^^^^^^^^^^^^^^^^^^^^ + + with: { type: "json" } +>type : error + +}); + +// Import type with 'with' keyword and invalid expression (should error) +type T10 = typeof import("./wat.mjs", { +>T10 : typeof import("wat") +> : ^^^^^^^^^^^^^^^^^^^^ + + with: { field: 123 } +>field : error + +}); + diff --git a/tests/cases/compiler/importTypeAttributesNonString.ts b/tests/cases/compiler/importTypeAttributesNonString.ts new file mode 100644 index 0000000000000..bc117956e78af --- /dev/null +++ b/tests/cases/compiler/importTypeAttributesNonString.ts @@ -0,0 +1,59 @@ +// @module: nodenext +// @filename: wat.mts +export const x = 1; + +// @filename: mod.mts + +// Import type with function expression (should error) +type T1 = typeof import("./wat.mjs", { + assert: { + a: (() => { + console.log("why can you write js here????"); + })(), + } +}); + +// Import type with number literal (should error) +type T2 = typeof import("./wat.mjs", { + assert: { field: 0 } +}); + +// Import type with template literal (should error) +type T3 = typeof import("./wat.mjs", { + assert: { field: `a` } +}); + +// Import type with regex (should error) +type T4 = typeof import("./wat.mjs", { + assert: { field: /a/g } +}); + +// Import type with array (should error) +type T5 = typeof import("./wat.mjs", { + assert: { field: ["a"] } +}); + +// Import type with object (should error) +type T6 = typeof import("./wat.mjs", { + assert: { field: { a: 0 } } +}); + +// Import type with method call (should error) +type T7 = typeof import("./wat.mjs", { + assert: { field: 0..toString() } +}); + +// Import type with valid string literal (should work) +type T8 = typeof import("./wat.mjs", { + assert: { type: "json" } +}); + +// Import type with 'with' keyword and valid string (should work) +type T9 = typeof import("./wat.mjs", { + with: { type: "json" } +}); + +// Import type with 'with' keyword and invalid expression (should error) +type T10 = typeof import("./wat.mjs", { + with: { field: 123 } +}); diff --git a/tests/cases/fourslash/organizeImportsAttributes4.ts b/tests/cases/fourslash/organizeImportsAttributes4.ts index bded70d338c6b..a04661551ec3c 100644 --- a/tests/cases/fourslash/organizeImportsAttributes4.ts +++ b/tests/cases/fourslash/organizeImportsAttributes4.ts @@ -6,16 +6,14 @@ ////import { E } from "./a" assert { foo: 'bar', bar: "foo" }; ////import { C } from "./a" assert { foo: "bar", bar: "foo" }; ////import { F } from "./a" assert { foo: "42" }; -////import { Y } from "./a" assert { foo: 42 }; ////import { Z } from "./a" assert { foo: "42" }; //// -////export type G = A | B | C | D | E | F | Y | Z; +////export type G = A | B | C | D | E | F | Z; verify.organizeImports( `import { A, B } from "./a" assert { foo: "foo", bar: "bar" }; import { C, D, E } from "./a" assert { bar: "foo", foo: "bar" }; import { F, Z } from "./a" assert { foo: "42" }; -import { Y } from "./a" assert { foo: 42 }; -export type G = A | B | C | D | E | F | Y | Z;`); \ No newline at end of file +export type G = A | B | C | D | E | F | Z;`); \ No newline at end of file