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');
在coffeescript中,你可以访问条件存在操作符,所以可以这样做:
param.lastName ?= 'smith'
编译成javascript:
if (param.lastName == null) {
param.lastName = 'smith';
}