export type HalfSpin = {
halfspin: string
}
export type FullSpin = {
fullspin: string
}
export type SpinType =
| HalfSpin
| FullSpin
export function isHalfSpin(_: SpinType): boolean {
return ((_ as HalfSpin).halfspin) !== undefined);
}
I encounter several scenarios where certain types have a single unique property, and I aim to refine the type from a union type. Is there an approach I can adopt to achieve this with more precision? Without having to manually test for its specific property in each type.
I am considering creating another type specifically for property names and utilizing it somehow.
Perhaps something along these lines:
function narrowByProperty<A, B extends A>(property: string) {
return (_: A): boolean => {
return (property in _)
};
}