-
Notifications
You must be signed in to change notification settings - Fork 13k
Closed
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
The error described below started with TypeScript 4.6.1-rc. A minimal repro is included.
π Search Terms
- Destructuring
- Union types
π Version & Regression Information
- This changed between versions 4.5.5 and 4.6.1-rc
β― Playground Link
Playground link with relevant code
π» Code
See this toy NAND evaluator. Something about the recursive union type causes TS to fail when destructuring the array. Deleting the default
clause causes the error to go away. (The default
clause is unnecessary if assuming TS is sound but one can see why a safety net is helpful.)
export type Expression = BooleanLogicExpression | 'true' | 'false';
export type BooleanLogicExpression = ['and', ...Expression[]] | ['not', Expression];
export function evaluate(expression: Expression): boolean {
if (Array.isArray(expression)) {
const [operator, ...operands] = expression;
switch (operator) {
case 'and': {
return operands.every((child) => evaluate(child));
}
case 'not': {
return !evaluate(operands[0]);
}
default: {
throw new Error(`${operator} is not a supported operator`);
}
}
} else {
return expression === 'true';
}
}
π Actual behavior
The line const [operator, ...operands] = expression;
produces the error: TS2488: Type 'never' must have a '[Symbol.iterator]()' method that returns an iterator.
π Expected behavior
Expected there not to be an TS error.
and0p
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