-
Notifications
You must be signed in to change notification settings - Fork 13k
Closed as not planned
Closed as not planned
Copy link
Labels
Not a DefectThis behavior is one of several equally-correct optionsThis behavior is one of several equally-correct options
Description
Bug Report Feature Request?
π Search Terms
generics conditional types narrowing
π Version & Regression Information
- This is the behavior in every version I tried, and I reviewed the FAQ for entries about Generics, Type System Behavior
β― Playground Link
Playground link with relevant code
π» Code
interface State {
a: string;
b: { value: string};
c: { value: string};
}
type NestedKey = "b" | "c";
type A<T> = T;
function f<T extends keyof State>(key: T, v: A<State[T]>) {
}
type B<T> = T extends object ? T : never;
function g<T extends keyof State>(key: T, v: B<State[T]>) {
}
// this works
function ex1<T extends NestedKey>(key: T) {
f(key, {value: ''});
}
// this doesn't
function ex2<T extends NestedKey>(key: T) {
g(key, {value: ''}); // this should not throw an error but it does
}
π Actual behavior
It seems when a conditional type is used, the compiler isn't evaluating the constraint of the given type parameter and instead falling back to the constraint defined locally.
π Expected behavior
The arguments type should be narrowed using the most constrained generic regardless of conditional types
Metadata
Metadata
Assignees
Labels
Not a DefectThis behavior is one of several equally-correct optionsThis behavior is one of several equally-correct options