One way to assert that an object has a certain property is by using the `!.` syntax as shown below:
type Person = {
name: string;
age: number;
gender?: string;
}
const myPerson: Person = {
name: 'John Cena',
age: 123,
gender: 'sth'
}
const myFunction = (person: Person) => {
checkIfPersonHasGender(person);
return person!.gender;
}
But how do we apply this for properties with keys in quotations like the example below? :
type Person = {
name: string;
age: number;
"my-gender"?: string;
}
I attempted to use
myPerson!.["my-gender"]
, however TypeScript returned an error saying Identifier expected
.