如何填充正在填充的类型具有函数的类型数组(getReadableSchedule)?如果我删除该功能,这是有效的。这是因为我分配数组元素的方式失败了吗?
src / app / mock-extracts.ts(3,14)中的错误:错误TS2322:类型'{id:number;中缺少属性'getReadableSchedule' name:string;描述:字符串;客户端:字符串;供应商:字符串; extractType:str ...'。
export class Extract {
id: number;
name: string;
description: string;
client: string;
vendor: string;
extractType: string;
path: string;
sentDate: string;
sentTo: string;
lastRun: string;
nextRun: string;
schedule: string;
getReadableSchedule(): string {
return "<return readable cronjob schedule>";
}
}
import { Extract } from "./extract";
export const EXTRACTS: Extract[] = [
{
id: 1,
name: "Find Barb",
description: "Find Barb in the unspide down.",
client: "Tower, Inc",
vendor: "Vendy",
extractType: "Normal",
sentDate: "Today",
path: "//outside",
sentTo: "",
lastRun: "",
nextRun: "",
schedule: ""
},
{
id: 2,
name: "Rescue Will",
description: "Give Will a hair cut.",
client: "Tower, Inc",
vendor: "Vendy",
extractType: "Normal",
sentDate: "Today",
path: "//outside",
sentTo: "",
lastRun: "",
nextRun: "",
schedule: ""
},
{
id: 3,
name: "Sooth Harry's Scar",
description: "Put Robitussin on Harry's scar.",
client: "Tower, Inc",
vendor: "Turkish Barn, LLC",
extractType: "Normal",
sentDate: "Today",
path: "//outside",
sentTo: "",
lastRun: "",
nextRun: "",
schedule: ""
}
];
答案 0 :(得分:4)
发生错误是因为数组中包含的对象文字与提取类的完全类型结构不匹配。
第一个选项:
要使其仅使用一些更改,请在每个对象中添加键 getReadableSchedule 的最后一个属性,并将其指向类原型中的方法:
500
第二个选项:
只需为每个对象创建一个实例,将其赋值给变量,为其赋值,然后将该变量添加到数组中,以这种方式创建的所有对象都可以访问该方法,因此不需要进行其他更改:
{
id: 1,
name: "Find Barb",
description: "Find Barb in the unspide down.",
client: "Tower, Inc",
vendor: "Vendy",
extractType: "Normal",
sentDate: "Today",
path: "//outside",
sentTo: "",
lastRun: "",
nextRun: "",
schedule: "",
getReadableSchedule: Extract.prototype.getReadableSchedule// < -this will point to the same method in the class.
},
第三种选择:
如果无意将该类用作构造函数,则使用接口可能更有意义。因此,在类之外,声明一个等同于类方法的简单函数...
const EXTRACTS: Extract[] = [];
let a = new Extract();
a.propA = ...;
a.propB = ...;
.
.
.
EXTRACTS.push(a);
let b = new Extract();
b.propA = ...;
.
.
.
EXTRACTS.push(b);
在课程中,删除方法的主体,只留下签名
export function getReadableSchedule(): string {
return "<return readable cronjob schedule>";
}
并在类型声明中从 类 更改为 界面 ,然后将其导出。
getReadableSchedule(): string;
现在像以前一样将对象文字添加到EXTRACT数组中,唯一需要更改的是导入 getReadableSchedule 并将其添加到每个对象:
export interface Extract {
.
.
.
}
TypeScript类型系统仅检查类型的结构。所以上课......
import { Extract, getReadableSchedule } from "./extract";
const EXTRACTS: Extract[] = [
{
id: "whatever",
...,
...,
...,
getReadableSchedule // <- this will point to the imported function
} //<- and now all the properties are compatible with the type interface
];
具有以下类型结构......
class Extract {
name: string;
getReadableSchedule() {
return "Some message";
}
}
要为上述类型的某个变量指定对象文字, 该文字必须包含该类型中的每个属性,而不是其他属性。
{
name: string;
getReadableSchedule(): string;
}
答案 1 :(得分:0)
您可以在函数之前插入static
export class Extract {
....
static getReadableSchedule(): string {
return "<return readable cronjob schedule>";
}
}
在这种情况下,您不能在此函数中使用实例,因此您可能会更改为:
static getReadableSchedule(e: Extract ): string {
return "<return readable cronjob schedule>";
//use e
}
答案 2 :(得分:0)
class Foo {
id!: number;
firstName!: string;
lastName!: string;
get fullname(): string {
return `${this.firstName} ${this.lastName}`;
}
};
const INTERNAL_FOOS = [{...},{...}];
const FOOS: Foo[] = INTERNAL_FOOS.map(x => Object.assign(Foo.prototype, x));