-
Notifications
You must be signed in to change notification settings - Fork 13k
Closed
Closed
Copy link
Labels
BugA bug in TypeScriptA bug in TypeScriptFix AvailableA PR has been opened for this issueA PR has been opened for this issue
Milestone
Description
Bug Report
🔎 Search Terms
variadic, tuple, literal, spread
🕗 Version & Regression Information
First saw in v4.9.5; have tested back to 4.4.4 and still present.
- This is the behavior in every version I tried, and I reviewed the FAQ for entries about variadic.
⏯ Playground Link
💻 Code
const ok: [...string[], number] = ['a', 'b', 1];
// @ts-expect-error
const unexpected: [...string[], '!'] = ['a', '!'];
const workaround: [...string[], '!'] = ['a', '!' as const];
const works: [string, '!'] = ['a', '!'];
🙁 Actual behavior
For Variadic Tuples that end in a literal type, assigning a matching Tuple gives
Type '[string, string]' is not assignable to type '[...string[], "!"]'.
Type at position 1 in source is not compatible with type at position 1 in target.
Type 'string' is not assignable to type '"!"'.
🙂 Expected behavior
For normal Tuples and Variadic Tuples that only begin with a literal type, assignments do not need as const
to prevent widening.
For example this works:
const works2: ['!', ...string[], ] = ['!', 'x'];
It would be more intuitive if all Tuples behaved in the same way.
Metadata
Metadata
Assignees
Labels
BugA bug in TypeScriptA bug in TypeScriptFix AvailableA PR has been opened for this issueA PR has been opened for this issue