[英]Assigning a nested interface in typescript
I am trying to assign a value to a nested interface in typescript but the compiler seems to throw an error even though the format matches correctly我正在尝试为 typescript 中的嵌套接口分配一个值,但即使格式正确匹配,编译器似乎也会抛出错误
type wheelT = [contemp: number, motemp: number, error: string];
interface getData {
frontLeft: wheelT[];
frontRight: wheelT[];
rearLeft: wheelT[];
rearRight: wheelT[];
voltage: number;
amps: number;
mode: string;
FWD: string;
}
initjson: getData = {
frontLeft: [0, 0, "000"],
frontRight: [0, 0, "000"],
rearLeft: [0, 0, "000"],
rearRight: [0, 0, "000"],
voltage: 0,
amps: 0,
mode: "--",
FWD: "--",
};
essentially throws an error at frontLeft, frontRight, rearLeft, rearRight saying it is not assignable.本质上在 frontLeft、frontRight、rearLeft、rearRight 抛出一个错误,说它是不可分配的。
Your type does not match correctly.您的类型不正确匹配。 Your
wheelT
type is a tuple with 3 entries.您的
wheelT
类型是一个包含 3 个条目的元组。 frontLeft
(as well as frontRight, rearLeft, rearRight) is of type wheelT[]
- so an array of tuples. frontLeft
(以及 frontRight、rearLeft、rearRight)的类型是wheelT[]
- 所以是一个元组数组。 Change this to wheelT
(without the array brackets) and it should work.将其更改为
wheelT
(不带数组括号),它应该可以工作。
Without the array brackets, getData
looks like this:没有数组括号,
getData
看起来像这样:
interface getData {
frontLeft: wheelT
frontRight: wheelT;
rearLeft: wheelT;
rearRight: wheelT;
voltage: number;
amps: number;
mode: string;
FWD: string;
}
Here the working TS Playground .这里是工作 TS Playground 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.