You can use cast ( (<T>obj).x
where T
is the type, and obj
is your object instance to access property/method x
).
In your case you defined file as JSX which try to pars all <x>
to a tag, so you can use as
operator (obj as T).x
, that convert your object.
if your object inherit a specific type, or it compiles to JS and later the type doesn't matter, you can even cast to your desired type, but if you do not do that, you have no idea about the type, and your function is simple, won't trouble you later, you may use any
instead of T
.
function aaa(cell: unknown): void {
if(cell && typeof cell == 'object' && cell.hasOwnProperty('display') && 'display' in cell) {
console.log((cell as any).display)
}
}
aaa({display: 'abc'})
As you can see the type are different but it is parsable
class MyType {
public display:string|undefined;
}
function aaa(cell: unknown): void {
if(cell && typeof cell == 'object' && cell.hasOwnProperty('display') && 'display' in cell) {
console.log((cell as MyType).display)
}
}
class XyzType {
public constructor(display:string){
this.display = display;
}
public display:string|undefined;
}
aaa(new XyzType('abc'))