打字稿装饰器限制基于第二功能类型的第一功能类型

时间:2020-06-17 14:23:58

标签: typescript typescript-typings

我想输入TS Class属性装饰器,这样第一个函数将受到第二个函数中键名的限制,如下所示:

interface A {
    name: "a" | "b";
    surname: "c";
}

const x2 = <K extends keyof A, Type extends A[K]>(val: Type) => (key: K) => {}
x2("aaa")("surname"); // "aaa" should be type "c" but is "a" | "b" | "c"

它在这里正常工作,但仅在一个功能中起作用

const x = <K extends keyof A, Type extends A[K]>(key: K, val: Type) => { }
x("surname", "xxx"); // "xxx" is limited to "c"

在最终版本中,我想在装饰类型中使用它:

export const MyDecorator = <
    S extends State,
    Key extends keyof MyInterface,
    Type extends MyInterface[Key]
>(
    defaultValue: Type
) => (target: S, key: Key) => {
};

    @MyDecorator("li") // limit the default value here
    something: Something;

https://www.typescriptlang.org/play/?ssl=9&ssc=1&pln=10&pc=21#code/JYOwLgpgTgZghgYwgAgILIN4Chm+SOAWwgC5kAiOc5AHwoCNyBuHPAZwFcoDizyFmWAL5YsCAPYg2YZAA8ATMgC8yADwBpZBFmQQAEzbIA1hACe4mGgA0yACqmADim26DaANrqAugD4AFABucAA2ZPZOAJTKPsh+JqZk6lFKMRgiCn6UcFQRmZzcRBDkESxiktJyymqaLhD6hvEW1naOzjp1bqievnFmiTZBoS2R0ZjI6XlcPEU25LLzxUxAA

0 个答案:

没有答案