Property 'getReadableSchedule' is missing in type
The error happens because the object literals that are included in the array don't match the exact type structure of the Extract class.
First option:
To make it work with just a few changes, add as last property in each object the key getReadableSchedule and point it to method in the class prototype:
{
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.
},
Second option:
Just create an instance for each object, assign it to a variable, assign values to it's properties and then add that variable to the array, all objects created this way already have access to the method so no other change will be needed:
const EXTRACTS: Extract[] = [];
let a = new Extract();
a.propA = ...;
a.propB = ...;
.
.
.
EXTRACTS.push(a);
let b = new Extract();
b.propA = ...;
.
.
.
EXTRACTS.push(b);
Third option:
If there is no intention to use the class as constructor, problably makes more sense to use an interface. So , outside of the class, declare a simple function equivalent to the class method...
export function getReadableSchedule(): string {
return "<return readable cronjob schedule>";
}
Inside the class, remove the body of the method leaving only the signature
getReadableSchedule(): string;
and change from class to interface in the type declaration and then export it.
export interface Extract {
.
.
.
}
Now add object literals to EXTRACT array as done previously and the only thing that still is necessary to change is importing getReadableSchedule and adding it to each object:
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
];
The TypeScript type system checks only the structure of types. So the class...
class Extract {
name: string;
getReadableSchedule() {
return "Some message";
}
}
has the following type structure ...
{
name: string;
getReadableSchedule(): string;
}
To assign an object literal for some variable of the type above, that literal must have every property existent in the type and no other.
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;