gpt4 book ai didi

typescript - 类型 'Key' 不能用于索引类型 'Object'

转载 作者:行者123 更新时间:2023-12-01 22:14:37 30 4
gpt4 key购买 nike

试图用声音类型实现功能性的弹奏,但我做错了:

function pluck<Object extends {}, Key = keyof Object>(key: Key): (o: Object) => Object[Key] {
return object => object[key];
}

TS 错误:

Type 'Key' cannot be used to index type 'Object'

最佳答案

你想要Key extends keyof Object:

function pluck<Object extends {}, Key extends keyof Object>(key: Key): (o: Object) => Object[Key] {
return object => object[key];
}

泛型中的语法 T = S 指定类型的默认值,但不以任何方式限制类型。

关于typescript - 类型 'Key' 不能用于索引类型 'Object',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46846669/

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