export function createListItems<T>(data:any[]): T[]{
const items: T[] = [];
for (let i = 0; i < data.length; i++) {
let obj: Partial<Record<keyof T,string | number >>={};//must have age and name, string and number type
Object.keys(data[i]).map((key:string) => {
obj[key]=data[i][key];//taking address as well and boolean type as well
});
items.push(obj as T);
}
return items;
}
interface userDetails{
name:string;
age:number;
}
console.log(createListItems<userDetails>([{name:true,address:false},{name:"kailash",age:"string"}]);
In the above code obj should take only name and age but taking address property as well. It should accept only string and number types but accepting boolean type also. I want name should be of type only string and age should be of type only number. But for name property it is taking even boolean value as well dynamically. How can I check if dynamic key exists in interface or not and it value is matching with the type as it is defined in interface?