Typescript:类型'Ivalue [T]'不能用于索引类型'{'1':数字; '2':数字; '3':数字; }'

时间:2020-01-17 04:16:50

标签: typescript

我在typescript Playground中有一个演示

const C = {
    METHOD: {
        '1': 2,
        '2': 2,
        '3': 3
    },
    CCSIGNFLAG: {
        '4': 4,
        '5': 5,
        '6': 6
    }
};
interface IKey {
    method: '123123123';
    ccSignFlag: '2222';
    [key: string]: string;
}
interface Ivalue {
    method: '1' | '2' | '3';
    ccSignFlag: '4' | '5' | '6';
    [key: string]: string;
}

function test<T extends keyof IKey>(field: T, value: Ivalue[T]) {
    switch (field) {
        case 'method':
            return C['METHOD'][value];
    }
}
test('ccSignFlag', '4');

显示错误:Type 'Ivalue[T]' cannot be used to index type '{ '1': number; '2': number; '3': number; }'

我希望第一个参数约束第二个参数的输入

我该怎么办?

0 个答案:

没有答案