简体   繁体   English

keyof typeof 找不到名称

[英]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;
}

TS keyof typeof<object> 不允许 object.keys(<object> ) 被分配?<div id="text_translate"><p> 我不明白这段代码有什么问题。</p><p> 这是我的代码:</p><pre> export type SportsTypes = keyof typeof SportsIcons export const sports: SportsTypes[] = Object.keys(SportsIcons);</pre><p> 我正在将变量键入 object 的键。 但是,当我尝试将所述键的数组分配给这个变量时,我得到了 ts 错误: Type 'string[]' is not assignable to type '("Football" | "GameController" | "Tennis" | "Basketball" | "PingPong" | "Volleyball" | "HockeyPuck" | "Chess" | "AmericanFootball" | "Baseball" | "SandVolleyball")[]</p></div></object></object> - TS keyof typeof <Object> does not allow object.keys(<Object>) to be assigned?

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 三.js 文件错误,找不到名称“定义”,类型“typeof”上不存在属性“三”。 类型“{}”.etc 上不存在属性“set” - three.js file errors, Cannot find name 'define', Property 'THREE' does not exist on type 'typeof . Property 'set' does not exist on type '{}'.etc 找不到名称“ ReadableStream” - Cannot find name 'ReadableStream' 找不到名称符号 - Cannot find name Symbol 找不到名称“ BrowserWindow” - Cannot find name 'BrowserWindow' 找不到名称“模块” - Cannot find name 'module' 找不到名称&#39;withStyles - Cannot find name 'withStyles 找不到名称“ SafeUrl” - Cannot find name 'SafeUrl' 找不到长的名字 - Cannot Find Name Long T[keyof T] 不能用于索引 {} - T[keyof T] cannot be used to index {} TS keyof typeof<object> 不允许 object.keys(<object> ) 被分配?<div id="text_translate"><p> 我不明白这段代码有什么问题。</p><p> 这是我的代码:</p><pre> export type SportsTypes = keyof typeof SportsIcons export const sports: SportsTypes[] = Object.keys(SportsIcons);</pre><p> 我正在将变量键入 object 的键。 但是,当我尝试将所述键的数组分配给这个变量时,我得到了 ts 错误: Type 'string[]' is not assignable to type '("Football" | "GameController" | "Tennis" | "Basketball" | "PingPong" | "Volleyball" | "HockeyPuck" | "Chess" | "AmericanFootball" | "Baseball" | "SandVolleyball")[]</p></div></object></object> - TS keyof typeof <Object> does not allow object.keys(<Object>) to be assigned?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM