Skip to content

fix: disallow empty template literals in module import attribute types - #64243

Open
Cameron (camc314) wants to merge 2 commits into
microsoft:mainfrom
camc314:c/align-string-lit-behaviour
Open

fix: disallow empty template literals in module import attribute types#64243
Cameron (camc314) wants to merge 2 commits into
microsoft:mainfrom
camc314:c/align-string-lit-behaviour

Conversation

@camc314

Copy link
Copy Markdown
Contributor

Module declarations currently accept template literals without substitutions in import attribute types.

This behaviour is incorrect as every other case rejects these empty template literals.

import mycss from "./mycss.css" with { type: `css` };
//                                           ^^^^^ TS2858: Import attribute values must be string literal expressions.
import mycs2 from "./mycss.css" with { type: `css${''}` };
//                                           ^^^^^^^^^^ TS2858: Import attribute values must be string literal expressions.
type t1 = typeof import('foo', { with: { x: `` }})
//                                          ^^ TS2858: Import attribute values must be string literal expressions.
type t2 = typeof import('foo', { with: { x: `${''}` }})
//                                          ^^^^^^^ TS2858: Import attribute values must be string literal expressions.
declare module "*.css" with { type: `css` } {}
//                                   ^^^^^ Currently allowed.
declare module "*.css" with { type: `css${''}` } {}
//                                   ^^^^^^^^^^ TS1555: An import attributes property must have a string literal type annotation.

This PR now rejects the remaining case with TS1555, consistently requiring a quoted string literal.

Copilot AI balanced review requested due to automatic review settings September 11, 2026 10:39
@github-project-automation github-project-automation Bot moved this to Not started in PR Backlog Sep 11, 2026
@typescript-automation typescript-automation Bot added For Uncommitted Bug PR for untriaged, rejected, closed or missing bug labels Sep 11, 2026
@typescript-automation

Copy link
Copy Markdown

This PR doesn't have any linked issues. Please open an issue that references this PR. From there we can discuss and prioritise.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Copilot was unable to review this pull request because the user who requested the review has reached their quota limit.

@DanielRosenwasser

Copy link
Copy Markdown
Member

Hm, I'm not totally sure if the symmetry is necessary here - but the test coverage is at the least appreciated.

Do you know if we also have tests for template string types with interpolations? If not, can you add those here?

@camc314

Copy link
Copy Markdown
Contributor Author

Hm, I'm not totally sure if the symmetry is necessary here - but the test coverage is at the least appreciated.

You're right that the symmetry isn't technically necessary: `foo`, "foo" and 'foo' can all represent the same attribute value.

However, I think requiring quoted string literals here is the better design. ECMAScript explicitly distinguishes StringLiteral from NoSubstitutionTemplate ([spec](https://tc39.es/ecma262/multipage/ecmascript-language-lexical-grammar.html#sec-literals-string-literals)), and the other import attribute contexts already reject templates. Allowing them specifically in module declarations introduces an exception with no meaningful benifit.

this exception also impacts downstream tools. For example, we can no longer use StringLiteral for this type, as it would violate the invarient that all string literals are wrapped in either ' or ", hence we would have to introduce new syntax type and handle it appropriately. While I understand that ECMAScript doesn't dictate TypeScript's declaration syntax, I think consistency is sufficient reason to reject it here.

It's also worth noting, that since this is unreleased, we can change it without any breaking changes. If a usecase appears where it makes sense to use a TemplateLiterals here, it can easily be added.

Do you know if we also have tests for template string types with interpolations? If not, can you add those here?

I couldn't find any existing coverage. I've added this as a test case in 77dbf49.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

For Uncommitted Bug PR for untriaged, rejected, closed or missing bug

Projects

Status: Not started

Development

Successfully merging this pull request may close these issues.

3 participants