-
>
The basic idea is, i have an object like:
```javascript
const reg = {
a: (p: string) => false,
b: (p: number) => 'hello'
};```
and im looking to make a function that can do like
```javascript
function run(
k: keyof reg,
p: Parameters<reg[k]>
): ReturnType<reg[k]> {
return reg[k](p);
}```
and i want all the beautiful typehinting to be automatic and happy
-
okay, what we want here is to narrow down the type of the object's value by the object's key, and thus safely use the object's value with an inferred type. & ofc have auto-completions for the correct type when we narrow it down.
-
hehe, i don’t think it’s possible yet. https://github.com/microsoft/TypeScript/issues/47648
-
> <...>
-
i’m excited about the upcoming PRs, even tho some have been there for a while already.
https://github.com/microsoft/TypeScript/pull/26349
https://github.com/microsoft/TypeScript/issues/26242
and fwiw, i tried playing w/ it [1] before looking for `typescript infer object value by key` & finding [2] - might be interesting. there’s typeof & keyof that are often used for similar stuff that is possible.
[1] https://www.typescriptlang.org/play?#code/FAYw9gdgzgLgBAJwKYHM4F44G87DnAQwC44AKABxNgQEsIUBK[…]Ko9Oc4N73DAguijF9bI-fov2GZ8JmmADUwOPMTkzMCcyA-Mrlgu4gA
[2] https://stackoverflow.com/questions/54598322/how-to-make-typescript-infer-the-keys-of-an-object-but-define-type-of-its-value#[[(hidden) GPVT2QZsF]]961[…]0_54598743