简体   繁体   English

元素隐式具有'any'类型,因为类型'xxx'没有索引签名.ts(7017)

[英]Element implicitly has an 'any' type because type 'xxx' has no index signature.ts(7017)

I am having an issue with TS. 我在TS上有问题。 I am getting the following error, for this branch of Code: 对于此代码分支,我得到以下错误:

The Interface: 接口:

export default interface IUser {
  username: string;
  email?: string;
  isActive: boolean;
  group: string[];
}

The interface that the name comes form: 名称来自的接口形式为:

interface Field {
  name: string;
  label: string;
  placeholder: string;
  help?: string | React.ReactNode;
}

ERROR: 错误:

Element implicitly has an 'any' type because type 'IUser' has no index signature.ts(7017)
const user: IUser

CODE: 码:

      mode === ActionMode.EDIT_INFO && user && user[name]
        ? {
            label: user[name],
            value: user[name]
          }
        : null;

I am reading in the TS Docs, the following: https://basarat.gitbooks.io/typescript/docs/types/index-signatures.html 我正在TS文档中阅读以下内容: https : //basarat.gitbooks.io/typescript/docs/types/index-signatures.html

Which led me to believe that I should do this: 这使我相信我应该这样做:

    const defaultValue =
      mode === ActionMode.EDIT_INFO && user && user[name.toString()]
        ? {
            label: user[name.toString()],
            value: user[name.toString()]
          }
        : null;

But it didn't help. 但这没有帮助。 Can you explain what is wrong here? 您能解释一下这里出什么问题吗? No can I not have something implicit here. 不,我不能在这里隐含一些东西。 Where should I reference the type? 我应该在哪里引用类型?

You can use index access in typescript to an object only if typescript can prove that the key you are using to access the object is a valid key for the type. 仅当打字稿可以证明您用来访问该对象的键是该类型的有效键时,才可以在打字稿中使用索引访问对象。 This means either the index argument is of a string literal type that is a key of the object or the object itself has a index signature: 这意味着index自变量是作为对象键的字符串文字类型,或者对象本身具有索引签名:

Index signature definition: 索引签名定义:

 export interface IUser {
    [n: string]: string | boolean | string[] | undefined;
    username: string;
    email?: string;
    isActive: boolean;
    group: string[];
}

interface Field {
    name: string;
    label: string;
    placeholder: string;
    help?: string | React.ReactNode;
}

declare let user: IUser;
declare let field: Field;
let value = user[field.name] // ok because of the index signature

Index argument is a union of keys: 索引参数是键的并集:

export interface IUser {
    username: string;
    email?: string;
    isActive: boolean;
    group: string[];
}

interface Field {
    name: keyof IUser;
    label: string;
    placeholder: string;
    help?: string | React.ReactNode;
}

declare let user: IUser;
declare let field: Field;
let value = user[field.name] // ok because of name is a property of IUser

I would recommend against the index signature as once you add that you can access the object with any key ( user.notHere is not an error with an index signature). 我建议不要使用索引签名,因为一旦添加,便可以使用任何键访问该对象( user.notHere不是索引签名的错误)。

If you already have a name field of type string and for some reason you can't change it, but you are reasonably sure it is a key of the interface you can use a type assertion: 如果您已经有一个string类型的name字段,并且由于某种原因您无法更改它,但是可以肯定地确定它是接口的键,则可以使用类型断言:

export interface IUser {
    username: string;
    email?: string;
    isActive: boolean;
    group: string[];
}

interface Field {
    name: string;
    label: string;
    placeholder: string;
    help?: string | React.ReactNode;
}

declare let user: IUser;
declare let field: Field;
let value = user[field.name as keyof IUser] // ok bacuse of the assertion

暂无
暂无

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

相关问题 错误 TS7017:对象类型的索引签名在表单验证角度 2 中隐式具有“任何”类型 - error TS7017: Index signature of object type implicitly has an 'any' type in form validation angular 2 元素隐式具有“ any”类型,因为类型“ xxx”没有索引签名 - Element implicitly has an 'any' type because type 'xxx' has no index signature element隐式具有“any”类型,因为类型“{0}”没有索引签名 - element implicitly has an 'any' type because type '{0}' has no index signature 打字稿:可能导致此错误的原因是什么? “元素隐式具有'任意'类型,因为类型'对象'没有索引签名” - Typescript: what could be causing this error? “Element implicitly has an 'any' type because type 'Object' has no index signature” TypeScript TS7015:元素隐式具有“任何”类型,因为索引表达式不是“数字”类型 - TypeScript TS7015: Element implicitly has an 'any' type because index expression is not of type 'number' TypeScript - ts(7053):元素隐式具有“任何”类型,因为“字符串”类型的表达式不能用于索引 - TypeScript - ts(7053) : Element implicitly has an 'any' type because expression of type 'string' can't be used to index TS 7015:元素隐式具有“任何”类型,因为索引表达式不是 Object 的“数字”类型。getElementsByTagName 的键 - TS 7015: Element implicitly has an 'any' type because index expression is not of type 'number' for Object.keys of getElementsByTagName document.form 中的 TS(7015):“元素隐式具有‘任意’类型,因为索引表达式不是“数字”类型 - TS(7015) in a document.form : "Element implicitly has an 'any' type because index expression is not of type "number" 错误 TS7052:元素隐式具有“任何”类型,因为类型“AbstractControl”没有索引签名。 你的意思是叫'get'吗? - error TS7052: Element implicitly has an 'any' type because type 'AbstractControl' has no index signature. Did you mean to call 'get'? 元素隐式具有“任何”类型,因为索引表达式不是“数字”类型。ts(7015) - Element implicitly has an 'any' type because index expression is not of type 'number'.ts(7015)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM