In my implementation, I have created a versatile type that can be either of another type or an interface, allowing me to utilize it in functions.
type Value = string | number
interface IUser { name: string, id: string }
export type IGlobalUser = Value | IUser;
When I require a function parameter to be of type IGlobalUser
, I naturally expect to access the properties of the interface IUser
. For example:
foo(user: IGlobalUser) { alert(user.name) }
Unfortunately, TypeScript throws an error:
Property 'name' does not exist on type 'string | number | IUser'.
Property 'name' does not exist on type 'string'.
I am aiming for the function to accept both Value
and IUser
seamlessly. Any suggestions on how to make this happen?