[英]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.