Reputation: 38352
class DetailsSummaryViewStore {
prop1: number;
prop2: string;
private setData(key: keyof DetailsSummaryViewStore, data: any ) {
this[key] = data;
}
}
In another method of this class I calling setData and am trying to set the values of the class properties after doing a api call. What is a right way to do this.
Current error is thrown in this[key]
Upvotes: 0
Views: 186
Reputation: 4775
That's strange, it seems to be related to having number properties.
This is fine:
class DetailsSummaryViewStore {
prop: string;
constructor() {
this.prop = 'stuff'
}
setData(key: keyof DetailsSummaryViewStore, data: any) {
this[key] = data;
}
}
This causes the error you are seeing:
class DetailsSummaryViewStore {
prop: number;
constructor() {
this.prop = 1
}
setData(key: keyof DetailsSummaryViewStore, data: any) {
this[key] = data;
// ERROR: Type 'any' is not assignable to type 'never'
}
}
Upvotes: 0
Reputation: 5529
I think its should be
class DetailsSummaryViewStore {
prop1: number;
prop2: string;
private setData(key: keyof DetailsSummaryViewStore, data: any ) {
(this[key] as any) = data;
}
}
Upvotes: 1