[英]Typescript 'T' cannot be used to index type? But T is "keyof" target type
type foo = {
asd:""
}
interface FOOS<T = keyof foo> extends foo {
copy(key: T): foo[T]// error
}
Type 'T' cannot be used to index type 'foo'.
How can I tell typescript that this T can be used as a key for that type?我如何告诉打字稿这个 T 可以用作该类型的键?
Your generic type has a default of keyof foo
, but someone could still instantiate it with any unrelated type: FOOS<string>, FOOS<unknown>
etc.您的泛型类型的默认值是keyof foo
,但有人仍然可以使用任何不相关的类型实例化它: FOOS<string>, FOOS<unknown>
等。
Try placing a restriction on T
:尝试对T
施加限制:
interface FOOS<T extends keyof foo= keyof foo> extends foo {
copy(key: T): foo[T]// error
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.