[英]keyof typeof cannot find name
I attempted to provide a type definition that defines an object as any
, because as of now it's fairly open-ended as to what it could be.我试图提供一个类型定义,将 object 定义为
any
,因为到目前为止,它是什么是完全开放的。 As well, it has a key for a property within this object.此外,它还具有此 object 中的一个属性的密钥。
type Props = {
obj: any;
objKey: keyof typeof obj;
};
The result is an error on the line for objKey
stating:结果是
objKey
行中的错误,指出:
Cannot find name 'obj'
If I use this inline with the function arguments, it works without issue as seen below.如果我将它与 function arguments 一起使用,它可以正常工作,如下所示。
({ obj, objKey }: { obj: any; objKey: keyof typeof obj })
Why would this error/warning by produced in the lone type definition, and not the inline argument typing?为什么这个错误/警告会在单独的类型定义中产生,而不是在内联参数键入中产生?
You should use a generic type parameter:您应该使用泛型类型参数:
type Props<P> = {
obj: P;
objKey: keyof P;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.