[英]How can I create interface in Typescript for array
I want to create an interface in typescript to receive this values: 我想在打字稿中创建一个接口以接收此值:
{
"ABANDONED VEH": 4,
"ADMINISTRATION": 4,
"ALARM-BUS/RES": 4
}
When I try to use the correct interface that I create, I'm getting the error: 当我尝试使用自己创建的正确接口时,出现错误:
Type string is not assignable to
IMapping
类型字符串不可分配给
IMapping
What I'd already tried: 我已经尝试过的:
export interface IMapping {
[key: string]: { value: string };
}
export interface IMapping {
key: any;
}
[key: string]: string;
PS. PS。 Here's my real code:
这是我的真实代码:
file interface.ts: 文件interface.ts:
export interface IMapping {
[key: string]: number;
}
file that use this interface: 使用此接口的文件:
import { IMapping } from '../interfaces';
const mapping: IMapping = '{'
+ '"ABANDONED VEH": 4,'
+ '"ADMINISTRATION": 4"}';
According to the shape of your data, the value type in your interface should be number
and not { value: string }
: 根据数据的形状,界面中的值类型应为
number
而不是{ value: string }
:
export interface IMapping {
[key: string]: number;
}
Or if you want to specify the properties: 或者,如果您想指定属性:
export interface IMapping {
'ABANDONED VEH': number,
'ADMINISTRATION': number,
'ALARM-BUS/RES': number
}
However, according to your error message and your code, you are assigning a string to an object of type IMapping
which is invalid. 但是,根据您的错误消息和代码,您正在为无效类型为
IMapping
的对象分配一个字符串。
Type string is not assignable to IMapping
类型字符串不可分配给IMapping
const mapping: IMapping = '{'
+ '"ABANDONED VEH": 4,'
+ '"ADMINISTRATION": 4"}';
// ^ remove this and use JSON.parse()
If you know the object at compile-time, then use a literal expression to define it: 如果您在编译时知道该对象,则使用文字表达式对其进行定义:
const mapping: IMapping = {
"ABANDONED VEH": 4,
"ADMINISTRATION": 4,
"ALARM-BUS/RES": 4
}
If your data comes from an API in a string format, then use JSON.parse() on it: 如果您的数据来自字符串格式的API,请在其上使用JSON.parse() :
const mapping: IMapping = JSON.parse('{"ABANDONED VEH": 4,"ADMINISTRATION": 4,"ALARM-BUS/RES": 4}');
Note however that your string is not valid JSON, you have a trailing "
after the last 4
. 但是请注意,您的字符串不是有效的JSON,在最后
4
字符后有尾随"
。
This looks like the typing for your data. 这看起来像是在输入数据。
interface IMapping {
[key: string]: number;
}
const data: IMapping = {
"ABANDONED VEH": 4,
"ADMINISTRATION": 4,
"ALARM-BUS/RES": 4
};
however, is not clear why you're talking about arrays
(indexeed ones), in that case you might want a list of IMapping
但是,不清楚为什么要谈论
arrays
(索引arrays
),在这种情况下,您可能需要一个IMapping
列表
const list: IMappings[] = [
{
"ABANDONED VEH": 4,
"ADMINISTRATION": 4,
"ALARM-BUS/RES": 4
},
{
"ABANDONED VEH": 5,
"ADMINISTRATION": 5,
"ALARM-BUS/RES": 5
},
];
Your values are numbers, not strings. 您的值是数字,而不是字符串。 You should try:
你应该试试:
export interface IMapping {
[key: string]: number;
}
Your second version with the any type will not work because you did not use the brackets - that will expect a property with name key , not a dictionary-like object. 您的第二个版本的any类型将无法使用,因为您没有使用方括号-这将期望使用名称为key的属性,而不是类似字典的对象。
EDIT : 编辑 :
After your edit, it is obvious that you are trying to assign a string. 编辑之后,很明显,您正在尝试分配一个字符串。 You should assign the object directly:
您应该直接分配对象:
const mapping: IMapping = {
"ABANDONED VEH": 4,
"ADMINISTRATION": 4};
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.