Thursday, April 5, 2018

If the fat arrow inside of a .then in TypeScript has only the one line for returning something, you don't need to wrap the line in curly braces or use the return keyword.

This...

.then(x => new Whatever(x));

 
 

...does the same thing as this:

.then(x => { return new Whatever(x); });

 
 

Addendum 4/6/2018: Remember if you do this simplification trick when newing up an object with curly braces that you need to wrap the curly braces in parenthesis. So this...

.then(x => { return {'foo':'bar','baz':'qux'} });

 
 

...becomes...

.then(x => ({'foo':'bar','baz':'qux'}) );

No comments:

Post a Comment