看看这个片段:
type Constrained<T extends { [K in string]: string }> = T;
type A = {
p: string;
}
interface B {
p: string;
}
type R1 = Constrained<A>; // Why this is OK...
type R2 = Constrained<B>; // ... and this an Error?
例如,我知道可以用不同的方式表达约束以避免错误,
type Constrained<T extends { [K in keyof T]: string }> = T;
我的问题是:为什么interface
失败了,而type
却没有失败?
我的猜测是因为interface
是开放式的(可以扩展/合并)。