Setting the default value for a TypeScript object passed as an argument

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 thought this might work:

 function sayName(params: {firstName: string; lastName: string = 'smith'}) { 

Obviously, if these were simple arguments, you could do this 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 you can:

 param.lastName ?= 'smith' 

What compiles in javascript:

 if (param.lastName == null) { param.lastName = 'smith'; } 
+56
typescript
Apr 26 '14 at 18:17
source share
5 answers

Actually, there seems to be a simple way now. The following code works in TypeScript 1.5:

 function sayName({first,last='Smith'}: {first: string; last?: string}){ var name = first + " " + last; alert(name); } sayName({firstName: 'Bob'}); 

The trick is to first bracket the keys you want to select from the argument object, with key=value for any default values. Follow this with : and type declarations.

This is slightly different from what you were trying to do, because instead of having an intact params object, you have dereferenced variables instead.

If you want to make it optional to pass anything to a function, add ? for all keys in this type and add the default value ={} after the type declaration:

 function sayName({first='Bob',last='Smith'}: {first?: string; last?: string}={}){ var name = first + " " + last; alert(name); } sayName(); 
+63
Sep 15 '15 at 21:45
source share

No, TypeScript has no natural way of setting default values โ€‹โ€‹for the properties of an object defined in this way, where it has a default value and the other does not. You can define a richer structure:

 class Name { constructor(public first : string, public last: string = "Smith") { } } 

And use this instead of defining an inline type.

 function sayName(name: Name) { alert(name.first + " " + name.last); } 

You cannot do something like this, unfortunately:

 function sayName(name : { first: string; last?:string } /* and then assign a default object matching the signature */ = { first: null, last: 'Smith' }) { } 

Once it sets the default if name was undefined .

+16
Apr 26 '14 at 19:15
source share

Typescript now supports default options:

https://www.typescriptlang.org/docs/handbook/functions.html

In addition, adding a default value allows you to omit the type declaration because it can be inferred from the default value:

 function sayName(firstName: string, lastName = "Smith") { const name = firstName + ' ' + lastName; alert(name); } sayName('Bob'); 
+11
Jul 06 '17 at 0:00
source share

Destructuring objects parameter object is something that many of the above answers strive for, and Typescript now has methods that make it much easier to read and intuitively understand.

Basics of destruction:. By destructuring an object, you can select the properties of the object by the key name. You can define both few and many of the properties that you like, and the default values โ€‹โ€‹are set using the basic syntax let {key = default} = object .

 let {firstName, lastName = 'Smith'} = myParamsObject; //Compiles to: var firstName = myParamsObject.firstName, _a = myParamsObject.lastName, lastName = _a === void 0 ? 'Smith' : _a; 

Writing an interface, type, or class for a parameter object improves readability.

 type FullName = { firstName: string; /** @default 'Smith' */ lastName ? : string; } function sayName(params: FullName) { // Set defaults for parameter object var { firstName, lastName = 'Smith'} = params; // Do Stuff var name = firstName + " " + lastName; alert(name); } // Use it sayName({ firstName: 'Bob' }); 
+9
Aug 24 '16 at 3:43 on
source share

This may be a good way to do this, which does not include long constructors.

 class Person { firstName?: string = 'Bob'; lastName?: string = 'Smith'; // Pass in this class as the required params constructor(params: Person) { // object.assign will overwrite defaults if params exist Object.assign(this, params) } } // you can still use the typing function sayName(params: Person){ let name = params.firstName + params.lastName alert(name) } // you do have to call new but for my use case this felt better sayName(new Person({firstName: 'Gordon'})) sayName(new Person({lastName: 'Thomas'})) 
+9
Oct. 30 '16 at 17:43
source share



All Articles