4 ms·
This is a moment where the postfix `!` operator comes in handy. It is a well hidden secret, and one that I'm not going to try to lookup the docs for on mobile,
by Klathmon 7y ago
This is a moment where the postfix `!` operator comes in handy.
It is a well hidden secret, and one that I'm not going to try to lookup the docs for on mobile, but the idea is that the operator strips null/undefined from the type of whatever is before it.
So you can do something like this:
const [ a, b ] = await Promise.all([
async () => ({ foo: 'bar' }),
() => null
])
console.log(a!.foo) // `a!` strips the nullable off `a`
- shhsshs 7y agoWhen I plug in your example (in Chrome console and in the TypeScript compiler), `a` is type `() => Promise<{foo: string}>` and `b` is type `() => any`. I thought you had to actually provide a Promise to `Promise.all`, not functions that return promises (you have to invoke the functions): const [ a, b ] = await Promise.all([ (async () => ({ foo: 'bar' }))(), (() => null)(), ]) When I do this, `a` and `b` have their proper types.
- Klathmon 7y agoAh good catch, I was spacing on the easiest way to show how to easiest show the operator and forgot to invoke my functions!