Answer the question
In order to leave comments, you need to log in
How to work with higher order functions?
Good afternoon, there is a higher order function that adds methods and properties to classes.
typescript
class Model {
prototype: any;
static find(id: number): number {
return id;
}
save(): boolean {
return true;
}
}
function foo<M extends Model>(model: M) { // Какой тип она должна вернуть ?
model.prototype.create = function (): void {
// do something
};
model.findAll = function (): number[] { // Error Property 'findAll' does not exist on type 'M'.
return [1, 2, 3];
}
return model;
}
class NewModel {
create(): void;
static findAll(): number[];
}
function foo<M extends Model>(model: M): NewModel { ... }
Answer the question
In order to leave comments, you need to log in
Expand obviously with the keyword extends
.
Everything else is just hacks and crutches. Typescript assumes that a class should not change on the fly in any way.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question