Files
tsgo/testdata/baselines/reference/submodule/compiler/arrayDestructuringInSwitch1.js

46 lines
1.3 KiB
TypeScript

//// [tests/cases/compiler/arrayDestructuringInSwitch1.ts] ////
//// [arrayDestructuringInSwitch1.ts]
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';
}
}
//// [arrayDestructuringInSwitch1.js]
export function evaluate(expression) {
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';
}
}