I was trying generic typing, but it was throwing the below error. I am new to typescript. Please help me to understand the below error.
Property 'city' does not exist on type 'Person | MyLocation'.
In the following example, I have given the type as Person or MyLocation. In the output, I am able to get the given(city/sex) value. However output.city gives me propery city doesn’t exit error. Also when i try to destruct const { city } = identity<Person | MyLocation>({city: ‘SA’, sex: ‘Male’}); it return same error
interface Person {
name: string;
age: number;
}
interface MyLocation {
city: string;
sex: string;
}
function identity<Type>(arg: Type): Type {
return arg;
}
const output = identity<Person | MyLocation>({city: 'SA', sex: 'Male'});
console.log(output.city)