类型中缺少属性'getReadableSchedule'

Roc*_*sad 10 typescript

如何填充正在填充的类型具有函数的类型数组(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>";
  }
}
Run Code Online (Sandbox Code Playgroud)
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: ""
  }
];
Run Code Online (Sandbox Code Playgroud)

小智 6

发生错误是因为数组中包含的对象文字与Extract类的确切类型结构不匹配。

第一种选择:

为了使其仅需少量更改即可工作,在每个对象中添加键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.
},
Run Code Online (Sandbox Code Playgroud)

第二种选择:

只需为每个对象创建一个实例,将其分配给变量,为其属性分配值,然后将该变量添加到数组,以这种方式创建的所有对象已经可以访问该方法,因此无需进行其他更改:

const EXTRACTS: Extract[] = [];

let a = new Extract();
a.propA = ...;
a.propB = ...;
.
.
.

EXTRACTS.push(a);

let b = new Extract();
b.propA = ...;
.
.
.
EXTRACTS.push(b);
Run Code Online (Sandbox Code Playgroud)

第三种选择:

如果不打算将类用作构造函数,则使用接口可能更有意义。因此,在类之外,声明一个等效于类方法的简单函数...

export function getReadableSchedule(): string { 

    return "<return readable cronjob schedule>";
}
Run Code Online (Sandbox Code Playgroud)

在类内部,删除方法主体,仅保留签名

getReadableSchedule(): string; 
Run Code Online (Sandbox Code Playgroud)

并在类型声明中从更改为接口,然后将其导出。

export interface Extract {
    .
    .
    .
}
Run Code Online (Sandbox Code Playgroud)

现在像以前一样将对象文字添加到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
];
Run Code Online (Sandbox Code Playgroud)

TypeScript类型系统仅检查类型的结构。所以这堂课

class Extract {
    name: string;
    getReadableSchedule() {
        return "Some message";
    }        
}
Run Code Online (Sandbox Code Playgroud)

具有以下类型结构...

{
    name: string;
    getReadableSchedule(): string;
}
Run Code Online (Sandbox Code Playgroud)

要为上述类型的某个变量分配对象常量,则该常量必须具有该类型中存在的每个属性,并且没有其他属性。

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;
Run Code Online (Sandbox Code Playgroud)