2017-07-30 29 views
0

这是我喜欢的类型定义代码:为什么不能使用某些领域和keyof在类型定义

interface Foo { 
    a: string; 
    b: number; 
    c: boolean; 
} 

type Bar = { 
    [prop in keyof Foo]?: number | string; 
}; 

type Bar2 = { 
    id: string; 
    [prop in keyof Foo]?: number | string; 
}; 

Bar定义是好的,但也有一些是错误的Bar2,有没有什么办法来解决这个 ?

回答

2

我不知道为什么[K in keys]语法不能用经典的按键/类型定义混合,但你可以这样做:

type Bar2 = Bar & { 
    id: string 
} 
相关问题