Search Terms
contextual type await void
Suggestion
When contextually typing an await whose expression is unused, we should use the contextual type of void:
// "today" assumes https://github.com/microsoft/TypeScript/pull/39817
async function foo() {
// today: Promise<unknown>
// proposed: Promise<void>
await new Promise(resolve => {
// today: (value: unknown) => void
// proposed: (value: void) => void
resolve();
});
}
A statement like await new Promise(...) is commonly used to adapt a callback-based API where the result isn't value (such as Node's fs.writeFile). Once #39817 merges, the value argument for the resolve callback will no longer be optional unless it contains void. Since the result of the await expression above is unused, we should use void as the contextual type for the await, so that the contextual type for its operand becomes void | PromiseLike<void>, thus contextually typing the Promise construction.
Checklist
My suggestion meets these guidelines:
Search Terms
contextual type await void
Suggestion
When contextually typing an
awaitwhose expression is unused, we should use the contextual type ofvoid:A statement like
await new Promise(...)is commonly used to adapt a callback-based API where the result isn't value (such as Node'sfs.writeFile). Once #39817 merges, thevalueargument for theresolvecallback will no longer be optional unless it containsvoid. Since the result of theawaitexpression above is unused, we should usevoidas the contextual type for theawait, so that the contextual type for its operand becomesvoid | PromiseLike<void>, thus contextually typing thePromiseconstruction.Checklist
My suggestion meets these guidelines: