Suppose I find myself declaring a lot stuff as Foo|Bar|Baz
and I'm getting tired of writing that. How can I declare type Foob = Foo|Bar|Baz
so that I can start using Foob
instead?
A "union declaration" specifies a set of variable values and, optionally, a tag naming the union. The variable values are called "members" of the union and can have different types. Unions are similar to "variant records" in other languages.
TypeScript allows us to not only create individual types, but combine them to create more powerful use cases and completeness. There's a concept called “Intersection Types” in TypeScript that essentially allows us to combine multiple types.
Use declare var to declare variables. If the variable is read-only, you can use declare const . You can also use declare let if the variable is block-scoped.
Exactly as you typed.
type Foo = {};
type Bar = {};
type Ba = {};
declare type Foob = Foo|Bar|Ba;
let x:Foob;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With