我在 TypeScript 中遇到 switch 语句的问题
在多个代码编辑器上尝试过,我正在尝试使用 switch (true) 但由于某种原因代码在 switch 语句中失败。
const todoList: string[] | null = [];
function pushItemToTodoList(item: string) {
//COMPILES!!
if (todoList !== undefined && todoList !== null && todoList.length) {
if (todoList.length >= 5) {
console.log("LIST IS FULL!");
}
}
//DOESN'T COPMILE!!
switch (true) {
case todoList !== undefined && todoList !== null && todoList.length:
if (todoList.length >= 5) { //todoList is null???
console.log("LIST IS FULL!");
}
break;
}
}
pushItemToTodoList("clean house");
Helenr
小怪兽爱吃肉
相关分类