bobbyache

TypeScript - Method Parameters (default and optional)

Sep 27th, 2019
284
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. interface FullName {
  2.   firstName?: string,
  3.   lastName?: string
  4. }
  5.  
  6. function sayName(params: FullName = {}) {
  7.   const { firstName = 'Andrew', lastName = 'Smith' } = params;
  8.  
  9.   const name = firstName + ' ' + lastName;
  10.   console.log(name);
  11. }
  12.  
  13. sayName();
  14. sayName({ });
  15. sayName({ firstName: 'Shaun' });
  16. sayName({ firstName: 'Shaun', lastName: 'Greene' });
Advertisement
Add Comment
Please, Sign In to add comment