gpt4 book ai didi

typescript - 接口(interface)对象不能扩展 Record

转载 作者:行者123 更新时间:2023-12-03 16:32:23 25 4
gpt4 key购买 nike

为什么接口(interface)不能扩展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'

最佳答案

type T = Record<string, unknown>计算结果为 { [x: string]: unknown; }对象类型有 implicit index signature ,但接口(interface)没有( for safety reasons ),因此它们不扩展索引类型。

关于typescript - 接口(interface)对象不能扩展 Record<string, unknown>,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64621451/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com