-
Notifications
You must be signed in to change notification settings - Fork 13.1k
Open
Description
🔎 Search Terms
const type parameters inference deep function return type
🕗 Version & Regression Information
- This is the behavior in every version I tried from 5.0.4 up to 5.9.3 and nightly builds, and I reviewed the FAQ for entries about type inference and const type parameters
⏯ Playground Link
💻 Code
/**
* The trueCondition part of the conditional expression should never be reached and be a no-op,
* but if we get rid of the conditional expression or return anything other than TFunctions there,
* then inference breaks
*/
type IterateOverFunctions<TFunctions extends { [key: string]: () => void }> =
TFunctions extends never
? TFunctions // Inference breaks without this expression
: {
[K in keyof TFunctions]: TFunctions[K] extends () => infer TReturnType
? () => TReturnType
: never;
};
const asConstTyped = <
const TReturnType,
const TFunctions extends Record<string, () => TReturnType>,
>(
resolvers: IterateOverFunctions<TFunctions>,
) => resolvers;
const constTyped = asConstTyped({
Bank: () => ({
id: 'bank-1',
}),
});
// BankFn must resolve into `() => { readonly id: "bank-1"; }`
type BankFn = typeof constTyped.Bank;🙁 Actual behavior
Without the seemingly meaningless conditional expression in IterateOverFunctions type constTyped.Bank resolves into
() => unknown🙂 Expected behavior
IterateOverFunctions type without the conditional expression should resolve constTyped.Bank into the same narrowed down type as with it
() => {
readonly id: "bank-1";
}Additional information about the issue
The workaround in IterateOverFunctions doesn't work consistently and breaks when a more sophisticated iteration is going over the inferred types
Metadata
Metadata
Assignees
Labels
No labels