Can anyone help me figure out how to dynamically change object property values based on the property type in Typescript? I want to replace all string properties with "***", but I keep running into an error that says
Type '"***"' is not assignable to type 'T[keyof T]'
I've searched through the Typescript GitHub repository for a solution, but so far, I haven't had any luck.
As of writing this, I'm using the latest version of Typescript which is "3.8.2".
If you know the correct way to change object property values dynamically, especially when accessing properties like this, please share your insights!
Here's an example code snippet:
interface IClassA {
name: string;
age: number;
}
const user: IClassA = {
name: "Jhon Doe",
age: 20
};
obfuscate(user);
export function obfuscate<T extends IClassA>(obj: T) {
for (const prop of Reflect.ownKeys(obj) as (keyof T)[]) {
if (typeof obj[prop] === "string") {
obj[prop] = "***";
}
}
}