Let's say I have few interfaces A, B, C implementing common Base.
interface Base {
x: number;
y: number;
z: number;
}
interface A extends Base {
a: true;
}
interface B extends Base {
b: true;
}
interface C extends Base {
C: true;
}
And function with if statements:
function foo(arg: A|B|C){
if(arg.a!==undefined){//throws type error
//do stuff for type a
} else if(arg.b !== undefined){//throws type error
//do stuff for type b
} else if(arg.c !== undefined){ //throws type error
//do stuff for type c
}
}
How to correctly check if property exists? I don't wan't to use any type. Is //@ts-ignore
only option?
The hasOwnProperty() method will check if an object contains a direct property and will return true or false if it exists or not. The hasOwnProperty() method will only return true for direct properties and not inherited properties from the prototype chain.
The hasOwnProperty() method returns true if the specified property is a direct property of the object — even if the value is null or undefined . The method returns false if the property is inherited, or has not been declared at all.
Using includes() Method: If array contains an object/element can be determined by using includes() method. This method returns true if the array contains the object/element else return false. Example: html.
Objects have two types of properties: data and accessor properties.
Typescript will only allow access to common properties. Since the properties you test are not common to all members of the union, typescript will not let you access them.
You can use an in
type guard instead to test for the presence of the property.
interface Base {
x: number;
y: number;
z: number;
}
interface A extends Base {
a: true;
}
interface B extends Base {
b: true;
}
interface C extends Base {
C: true;
}
function foo(arg: A|B|C){
if('a' in arg){
arg.a
} else if('b' in arg){
arg.b
} else {
arg.C
}
}
You can use a type guard:
function isA(arg: A | B | C): arg is A {
return (<A>arg).a !== undefined;
}
function isB(arg: A | B | C): arg is B {
return (<B>arg).b !== undefined;
}
function foo(arg: A | B | C) {
if (isA(arg)) {
// do stuff for type a
} else if (isB(arg)) {
// do stuff for type b
} else {
// do stuff for type c
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With