interface A = {
name: string;
...
};
interface B = {
name: string;
...
};
interface C = {
key: string;
...
};
type UnionOfTypes = A | B | C | ...;
function hasName(item: UnionOfTypes) {
if ("name" in item) {
item; // typescript knows here that item is either A or B
}
}
Is there some way that I can infer types in the same way that if("name" in item)
does it? I don't have classes only interfaces/types.
So that I don't have to explicitly say
function hasName(item: UnionOfTypes): item is A | B {
...
}
The reasoning behind this is that I want to use other type guards deeper down, or is there some other reason why I should avoid narrowing down types this way?
There is no support for this by default in typescript. You can create a helper function that creates a type guard. The function passed to this helper function will return the guarded item, or
false
.Playground Link