Typescript 财产';getReadableSchedule';类型中缺少
如果要填充的类型具有函数(getReadableSchedule),如何填充类型化数组?如果我删除该函数,它将正常工作。这是否因为我分配数组元素的方式而失败 src/app/mock extracts.ts(3,14)中出错:错误TS2322:类型{id:number;name:string;description:string;client:string;vendor:string;extractType:str..中缺少属性“getReadableSchedule”Typescript 财产';getReadableSchedule';类型中缺少,typescript,Typescript,如果要填充的类型具有函数(getReadableSchedule),如何填充类型化数组?如果我删除该函数,它将正常工作。这是否因为我分配数组元素的方式而失败 src/app/mock extracts.ts(3,14)中出错:错误TS2322:类型{id:number;name:string;description:string;client:string;vendor:string;extractType:str..中缺少属性“getReadableSchedule” 导出类提取{ id:编号
导出类提取{
id:编号;
名称:字符串;
描述:字符串;
客户端:字符串;
供应商:字符串;
提取类型:字符串;
路径:字符串;
sentDate:字符串;
sentTo:字符串;
最后运行:字符串;
下一步:字符串;
附表:字符串;
getReadableSchedule():字符串{
返回“”;
}
}
从“/Extract”导入{Extract};
导出常量提取:提取[]=[
{
id:1,
名称:“找到倒钩”,
描述:“在未侧边下方找到倒钩。”,
客户:“塔楼公司”,
卖方:“卖方”,
提取类型:“正常”,
sentDate:“今天”,
路径://外部“,
sentTo:“,
上次运行:“,
下一代:“,
附表:“”
},
{
id:2,
名称:“救援意志”,
描述:“给威尔理发。”,
客户:“塔楼公司”,
卖方:“卖方”,
提取类型:“正常”,
sentDate:“今天”,
路径://外部“,
sentTo:“,
上次运行:“,
下一代:“,
附表:“”
},
{
id:3,
名字:“抚慰哈利的伤疤”,
描述:“把Robitussin放在哈利的伤疤上。”,
客户:“塔楼公司”,
供应商:“土耳其谷仓有限责任公司”,
提取类型:“正常”,
sentDate:“今天”,
路径://外部“,
sentTo:“,
上次运行:“,
下一代:“,
附表:“”
}
];
发生错误是因为数组中包含的对象文本与提取类的确切类型结构不匹配
第一种选择:
要使其只需少量更改即可工作,请在每个对象中添加键getReadableSchedule作为最后一个属性,并将其指向类原型中的方法:
{
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.
},
第三种选择:
如果不打算将类用作构造函数,那么使用接口可能更有意义
export function getReadableSchedule(): string {
return "<return readable cronjob schedule>";
}
并在类型声明中从类更改为接口,然后将其导出
export interface Extract {
.
.
.
}
现在,像前面一样添加对象文字以提取数组,唯一需要更改的是导入getReadableSchedule并将其添加到每个对象:
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
];
具有以下类型结构
{
name: string;
getReadableSchedule(): string;
}
要为上述类型的某个变量指定对象文字,
该文本必须具有类型中存在的所有属性,而不是其他属性
var fail1: Extract = { name: "1st failure" }; // does not type check - Missing property "getReadableSchedule"
var fail2: Extract = { getReadableSchedule() { return this.name; } }; // does not type check - Missing property "name";
var fail3: Extract = { // does not type check also!
name: "3rd failure", // Ok!
getReadableSchedule() { return this.name } //Ok!
id: 1 // Error - property "id" does not exist in type Extract
};
var success: Extract = {
name: "Success",
getReadableSchedule() { return "Ok!"}
}; // <- No errors;
// But it is ok to assign a Variable that has all properties existent
// in the type and additional ones
var notNamedType = {
name: "Also works",
getReadableSchedule() { return this.name },
id: 1 // property does not exist in the Extract type but...
}
let alsoWorks: Extract = notNamedType; // no casting needed and works as well;
var fail1:Extract={name:“1st failure”};//未键入check-缺少属性“getReadableSchedule”
var fail2:Extract={getReadableSchedule(){返回this.name;}};//未键入check-缺少属性“name”;
var fail3:Extract={//也不进行类型检查!
名称:“第三次失败”//Ok!
getReadableSchedule(){返回this.name}//确定!
id:1//错误-类型提取中不存在属性“id”
};
var成功:Extract={
名称:“成功”,
getReadableSchedule(){返回“Ok!”}
};//您可以在函数之前插入static
export class Extract {
....
static getReadableSchedule(): string {
return "<return readable cronjob schedule>";
}
}
导出类提取{
....
静态getReadableSchedule():字符串{
返回“”;
}
}
在这种情况下,您不能在此函数中使用您的实例,因此您可以更改为:
static getReadableSchedule(e: Extract ): string {//use e
return "<return readable cronjob schedule>";
}
静态getReadableSchedule(e:Extract):字符串{//使用e
返回“”;
}
- 定义你的类
- 为列表创建一个常量,只包含get/set变量,比如json内容
- 将常量映射到另一个可导出的常量,并将所有对象分配给类
- 访问链接以查看完整答案
- 享受
好的,您声明提取实例应该有一个getReadableSchedule方法,但是数组中没有一个对象有这个方法。因此,简而言之,您试图将苹果放在一个桔子数组中。@jbnite但您不填充方法,除非我不理解这个数组是如何填充对象列表的。是的,您可以当然可以。除非对象是类的实际实例:new Extract()
我对ts非常陌生。如果我给属性赋值,为什么这些条目不是类Extract
的实例呢?谢谢你的冗长回复。我结束了(就在今天早上)将格式移动到管道并删除方法。如果要使用任何实例属性,则不能是静态的。
var fail1: Extract = { name: "1st failure" }; // does not type check - Missing property "getReadableSchedule"
var fail2: Extract = { getReadableSchedule() { return this.name; } }; // does not type check - Missing property "name";
var fail3: Extract = { // does not type check also!
name: "3rd failure", // Ok!
getReadableSchedule() { return this.name } //Ok!
id: 1 // Error - property "id" does not exist in type Extract
};
var success: Extract = {
name: "Success",
getReadableSchedule() { return "Ok!"}
}; // <- No errors;
// But it is ok to assign a Variable that has all properties existent
// in the type and additional ones
var notNamedType = {
name: "Also works",
getReadableSchedule() { return this.name },
id: 1 // property does not exist in the Extract type but...
}
let alsoWorks: Extract = notNamedType; // no casting needed and works as well;
export class Extract {
....
static getReadableSchedule(): string {
return "<return readable cronjob schedule>";
}
}
static getReadableSchedule(e: Extract ): string {//use e
return "<return readable cronjob schedule>";
}
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));