【发布时间】:2020-11-01 07:39:24
【问题描述】:
为什么接口不能扩展Record
interface Data {
a: string
}
Data extends Record<string, unknown> ? 'yes' : 'no' // 'no'
但是,如果我将数据更改为类型,它可以正常工作
type Data {
a: string
}
Data extends Record<string, unknown> ? 'yes' : 'no' // 'yes'
【问题讨论】:
-
:) 奇怪的行为。即使您将有条件的
Data接口替换为DataAsType,这将是:type DataGeneric<T> = T; type DataAsType = DataGeneric<Data>;结果将是相同的。但是如果你用这个{[key in keyof Data]: Data[key]}替换Data,它会得到yes。
标签: typescript