Answer the question
In order to leave comments, you need to log in
How to extend class prototype in TypeScript after it's declared?
Good time. Help solve the problem:
There is a class
class Jodit {
constructor(options) {}
}
class Jodit {
static defaultOptions = {
dog: 100,
cat: 200
};
options: object
constructor(options?: object) {
this.options = extends(true, {}, Jodit.defaultOptions, options);
}
}
let editor = new Jodit({
dogs: 300
});
function Options() {
}
Options.prototype = {
dog: 100,
cat: 200
}
class Jodit {
options: object
constructor(options?: object) {
let __options = new Options();
this.options = extends(true, __options, options);
}
}
class Options {
dog = 100
cat = 200
}
class Jodit {
options: Options;
constructor(options?: object) {
let __options = new Options();
this.options = extends(true, __options, options);
}
}
Answer the question
In order to leave comments, you need to log in
You can use generics:
interface Options {
cats: number;
dogs: number;
}
const defaultOptions: Options = { cats: 100, dogs: 100 };
class Jodit<O extends Options> {
constructor(public options: O) {}
}
const jodit = new Jodit({ ...defaultOptions, mice: 1000 });
jodit.options.mice = 1024;
class Options {
cats = 100;
dogs = 100;
}
class Jodit<O extends Options> {
constructor(public options: O) {}
}
class MiceOptions extends Options {
mice = 1000;
}
const jodit = new Jodit(new MiceOptions());
jodit.options.mice = 1024;
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question