DEV Community

Discussion on: 9 tricks that separate a pro Typescript developer from an noob šŸ˜Ž

Collapse
 
hexshift profile image
HexShift

Nice! The tips on type inference and literal types are particularly handy, and I appreciate how you've broken things down into easily digestible bits that both newcomers and seasoned devs can benefit from. Shows just how much you can optimize your code with just a few key tricks. One suggestion I’d throw out there is a quick mention of using TypeScript with async/await patterns and sometimes the type inference can be a bit tricky there, so a little guidance would be useful.

Collapse
 
ruppysuppy profile image
Tapajyoti Bose

The only difference is when using async, the return type gets wrapped by a Promise

const asyncFn = async () => 0; // type AsyncFn = () => Promise<number>
Enter fullscreen mode Exit fullscreen mode

When you await, the Promise gets unwrapped & you can use it as a regular value

const func = async () => {
  const result = await asyncFn(); // type Result = number
}
Enter fullscreen mode Exit fullscreen mode

Everything else mentioned in the article still holds true regardless whether you are using Promises or not