I am facing a problem with the code snippet below and need help resolving it. Can anyone provide guidance on how to fix this issue?
type Data = {
id: number,
name: string
}
class Person {
id: number
name: string
constructor(personData: Data) {
this.id = personData.id
this.name = personData.name
}
update(updateData: Data) {
for (const prop in updateData) {
this[prop as keyof Data] = updateData[prop as keyof Data]
}
}
}
The Issue:
TS2322: Type 'string | number' is not assignable to type 'never'. Type 'string' is not assignable to type 'never'.
The crux of the problem lies in the update method:
this[prop as keyof Data] = updateData[prop as keyof Data]
. It seems that TypeScript is unable to determine that these properties are of the same type (string or number). While there is no error if all properties are of the same type, how can I explicitly specify to TypeScript that they share the same data type?