Open ddprrt opened 4 years ago
One more use case of https://github.com/microsoft/TypeScript/issues/27808 I guess
This definition works, but doesn't fail where it'd be nice to:
declare function get<Obj extends object, Keys extends string[]>(o: Obj, ...keys: Keys): CheckArguments<Obj, Keys>
/**
* Tests π₯
* */
const foo = get(obj, 'a') // "2"
const foo1 = get(obj, 'b', 'c') // 3
const foo2 = get(obj, 'b', 'd', 'e') // "string"
const foo3 = get(obj, 'b', 'd', 'f', 'g', 'h') // 4
// Want to fail, but doesn't
const foo4 = get(obj, 'x') // foo4: never
TypeScript Version: 4.2.0-dev.20201103
Search Terms: Recursive Conditional types, generics, tuple types
Code
Expected behavior:
Keys
gets bound to the value type passed as an argument to the function. This value type is then used for CheckArgumentsActual behavior:
Keys
is the entire union typeArguments<Obj>
, not the subset. This leads toCheckArguments
returning a too broad return type (and taking very long to evaluate ;-))Playground Link: Click here
Related Issues: Did not find any.