TypeScript: Infer type of generic keyof

倾然丶 夕夏残阳落幕 提交于 2021-02-20 00:30:00

问题


I have types like this:

type GenericType<T, K extends keyof T = keyof T> = {
    name: K;
    params: T[K]
}

type Params = {
    a: 1;
    b: 2;
}

const test: GenericType<Params> = {
    name: "a",
    params: 2
}

When I create an object like test that has property name: "a" I want the type of params to be inferred so that params must be 1. In my example params has type 1 | 2 which is Params[keyof Params]. But since name is "a" I think it should be possible to limit the type of params to just 1 without specifying the second generic type like const test: GenericType<Params, "a">. Basically what I want is:

type GenericType<T> = {
    name: keyof T;
    params: T[value of name]
}

Is this possible with typescript?


回答1:


Sure, you want GenericType<T> to be a union, which can be accomplished by mapping over the properties of T and then joining that mapped type into a union via lookup type:

type GenericType<T> = {
    [K in keyof T]: {
        name: K;
        params: T[K]
    }
}[keyof T];

Now you'll get the behavior you want, I think:

const test: GenericType<Params> = { // error!
    name: "a",
    params: 2
}

const good: GenericType<Params> = {
    name: "b",
    params: 2
}

You can verify that the type GenericType<Params> evaluates to { name: "a"; params: 1; } | { name: "b"; params: 2; }, so you're required to match the correct name with the correct params.

Okay, hope that helps; good luck!

Playground link to code



来源:https://stackoverflow.com/questions/60342467/typescript-infer-type-of-generic-keyof

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!