Closed
Description
π Search Terms
destructuring dependent types inside arrays
β Viability Checklist
- This wouldn't be a breaking change in existing TypeScript/JavaScript code
- This wouldn't change the runtime behavior of existing JavaScript code
- This could be implemented without emitting different JS based on the types of the expressions
- This isn't a runtime feature (e.g. library functionality, non-ECMAScript syntax with JavaScript output, new syntax sugar for JS, etc.)
- This isn't a request to add a new utility type: https://github.com/microsoft/TypeScript/wiki/No-New-Utility-Types
- This feature would agree with the rest of our Design Goals: https://github.com/Microsoft/TypeScript/wiki/TypeScript-Design-Goals
β Suggestion
Allow the typescript compiler to infer from the motivating example to infer p1 to be number
instead of number
| undefined
π Motivating Example
// TSCONFIG: noUncheckedIndexedAccess is set to true
// I like go's handling of fallible functions where it returns a tuple of the result and the potential error
// I want to make a good DX over Promise.allSettled using the go philosophy but with ts checking
async function myAllSettled<T extends readonly unknown[]>(
fn: () => T
) {
const promises = await Promise.allSettled(fn());
return promises.map((result) =>
result.status === 'fulfilled'
? [result.value, undefined]
: [undefined, new Error(String(result.reason))]
) as { [K in keyof T]: [Awaited<T[K]>, undefined] | [undefined, Error] };
}
async function test() {
const [[p1, p1Error], _,] = await myAllSettled(() => [
Promise.resolve(0), Promise.reject(1)
] as const);
if (p1Error) return;
// HELP!: How can I make (p1, p1Error) types be dependant so p1 can be inferred as `number` instead of `number | undefined`
// I want to keep the dx of inlining the destructuring in line 18
console.log(p1);
}
π» Use Cases
- I want to write a nice dx over Promise.allSettled returning a tuple of [value, error] such that they are dependent on each other
- I want to be able to preserve the inline destructuring so that typescript is able to infer the type of
p1
asnumber
instead ofnumber | undefined
- A workaround is to not inline the destructuring fully (see below) but comes at the cost of a extra line and a hit to dx
eg:
async function test() {
const [p1Result, _,] = await myAllSettled(() => [
Promise.resolve(0), Promise.reject(1)
] as const);
// Destructure again on a new line
const [p1, p1Error] = p1Result;
if (p1Error) return;
// p1 is now correctly inferred as `number` instead of `number | undefined`
console.log(p1);
}