function sayName(params: {firstName: string; lastName?: string}) {
params.lastName = params.lastName || 'smith'; // <<-- any better alternative to this?
var name = params.firstName + params.lastName
alert(name);
}
sayName({firstName: 'bob'});
我曾经想象过这样的事情可能会奏效:
function sayName(params: {firstName: string; lastName: string = 'smith'}) {
显然,如果这些是简单的论点,你可以这样做:
function sayName(firstName: string, lastName = 'smith') {
var name = firstName + lastName;
alert(name);
}
sayName('bob');
在咖啡脚本中,你可以访问条件存在运算符,所以可以这样做:
param.lastName ?= 'smith'
编译成 javascript:
if (param.lastName == null) {
param.lastName = 'smith';
}