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'});
I had imagined something like this might work:
function sayName(params: {firstName: string; lastName: string = 'smith'}) {
Obviously if these were plain arguments you could do it with:
function sayName(firstName: string, lastName = 'smith') {
var name = firstName + lastName;
alert(name);
}
sayName('bob');
And in coffeescript you have access to the conditional existence operator so can do:
param.lastName ?= 'smith'
Which compiles to the javascript:
if (param.lastName == null) {
param.lastName = 'smith';
}
params.lastName = params.lastName || 'smith';
is actually rather fine - it handles empty strings, undefined strings and null values. – Chenowethif(typeof x === "undefined") { … }
. Not that you don't know that, but just pointing out the general case for the OP. – PinchaslastName?: string
it can only ever be as SteveFenton said "handles empty strings, undefined strings and null values". – Galarzaparams.lastName = params.lastName || 'smith';
is the pattern I use – Inhumanityparam1: type = defaultValue
– Crinkleroot