Ich bin neu in Typoskript, und ich versuche, unsere Anwendung von es2016 in Typoskript umzuschreiben. Meine Aufgabe besteht darin, eine Klasse mit Dateneigenschaft zu haben und jedes Element aus dem Datenobjekt als Klasseneigenschaft verfügbar zu machen.Ist es möglich, dynamische Getter/Setter in Typoskript zu erstellen?
ich auf diesem JavaScript-Code fest: `` `
for(let key in this.data) {
Object.defineProperty(this, key, {
get: function(value:any) { return this.data[key]; },
set: function(value:any) {
if (this.data[key] !== value) {
this.data[key] = value;
this.updatedKeys.push(key);
}
},
});
}
` ``
Es ist ziemlich einfach Getter/Setter für Typoskript zu verwenden, aber ich verwirrt, wenn ich schaffen kann sie dynamisch?
`` `
interface IData {
id: number;
[propName: string]: any;
}
class Model {
protected updatedKeys:string[] = [];
baseUrl:string = null;
data:IData;
fields:IData;
constructor(data:IData={id:null}, fields:IData={id:null}) {
super()
this.data = data;
this.fields = fields;
for(let key in this.data) {
Object.defineProperty(this, key, {
get: function(value:any) { return this.data[key]; },
set: function(value:any) {
if (this.data[key] !== value) {
this.data[key] = value;
this.updatedKeys.push(key);
}
},
});
}
}
}
` ``
tsc -t ES2016 --lib "es2016","dom" models.ts
diesen Fehler geben:
models.ts(33,40): error TS2345: Argument of type '{ get: (value: any) => any; set: (value: any) => void; }' is not assignable to parameter of type 'PropertyDescriptor & ThisType<any>'. Type '{ get: (value: any) => any; set: (value: any) => void; }' is not assignable to type 'PropertyDescriptor'. Types of property 'get' are incompatible. Type '(value: any) => any' is not assignable to type '() => any'.
Und ich weiß nicht, wie loswerden von diesem Problem.
Vielen Dank für Hilfe im Voraus.