Skip to content

Variadic Tuples that end in a literal type require explicit as const narrowing #52760

@TrueWill

Description

@TrueWill

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.

Metadata

Metadata

Assignees

Labels

BugA bug in TypeScriptFix AvailableA PR has been opened for this issue

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions