Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}

Expand Down
30 changes: 1 addition & 29 deletions tests/baselines/reference/importAssertionNonstring.errors.txt
Original file line number Diff line number Diff line change
@@ -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.

Expand All @@ -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.

Expand Down
Original file line number Diff line number Diff line change
@@ -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() };
Expand Down
Original file line number Diff line number Diff line change
@@ -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() };
Expand Down
Original file line number Diff line number Diff line change
@@ -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() };
Expand Down
65 changes: 65 additions & 0 deletions tests/baselines/reference/importTypeAttributesNonString.js
Original file line number Diff line number Diff line change
@@ -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 {};
Loading