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
https://www.typescriptlang.org/play?ts=4.9.5#code/MYewdgzgLgBCDWAuGBtAdB6AnAlmA5igLoA0MYArgLYBGAplkTALyoDkAhm2WzdzAEYiAbgBQogPQSYAASgQAtHQAeABzrAoSrFhBZRoSLApgV6zXQAmydJii4CxHgEI2TVik782rkeMPQMADuevAcuibWqBho2HiEpDA+bizsXC5sMBwQMAFQfgbggSFY8BA2cQQZ7mnevmJAA
💻 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.
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.
⏯ Playground Link
https://www.typescriptlang.org/play?ts=4.9.5#code/MYewdgzgLgBCDWAuGBtAdB6AnAlmA5igLoA0MYArgLYBGAplkTALyoDkAhm2WzdzAEYiAbgBQogPQSYAASgQAtHQAeABzrAoSrFhBZRoSLApgV6zXQAmydJii4CxHgEI2TVik782rkeMPQMADuevAcuibWqBho2HiEpDA+bizsXC5sMBwQMAFQfgbggSFY8BA2cQQZ7mnevmJAA
💻 Code
🙁 Actual behavior
For Variadic Tuples that end in a literal type, assigning a matching Tuple gives
🙂 Expected behavior
For normal Tuples and Variadic Tuples that only begin with a literal type, assignments do not need
as constto prevent widening.For example this works:
It would be more intuitive if all Tuples behaved in the same way.