DEV Community

bin
bin

Posted on

12-Union Types in TS

when you are not sure the data type, instead of using any, we highly recommand you to use union.

but if you pass a union type to a function, you can't use a method that only apply to one type.

we can use this way:

we can also use union type in array, but all of the elememts must be share the same type, such as number or string.

so if you want to include multiple types into a single array, use a union type like this:

there is also a case that like enum in java:

Uploading image

Top comments (0)