You can join two types in TypeScript using the Union (|
) operator. The resulting type will be a type that can hold values of either of the two input types.
Here’s an example:
index.ts31 chars2 lines
In this example, MyType
is a union type that can hold both strings and numbers.
You can also use Type Aliases to join types:
index.ts39 chars2 lines
In this example, StringOrNumber
is a Type Alias for the union type string | number
.
You can then use this type alias throughout your code:
index.ts138 chars7 lines
gistlibby LogSnag