1、多重判断时使用 Array.includes
test = (fruit: string) => { if (fruit == ‘apple‘ || fruit == ‘strawberry‘....) { console.log(‘apple or strawberry‘); } }
test = (fruit: string) => { const fruits = [‘strawberry‘,‘apple‘]; if (fruits.includes(fruit)) { console.log(‘apple or strawberry‘); } }
2、使用默认参数和解构
在JavaScript中我们总是需要检查 null / undefined的值和指定默认值:
test = (fruit: string, quantity?: any) => { if (!fruit) { return; } console.log(quantity || 0) }
我们可以通过声明 默认函数参数
test = (fruit: string, quantity: any = 20) => { if (!fruit) { return; } console.log(quantity) }
原文:https://www.cnblogs.com/Nyan-Workflow-FC/p/10457526.html