Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to declare a new type from unions?

Tags:

typescript

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?

like image 512
mpen Avatar asked Apr 14 '16 06:04

mpen


People also ask

What is union type variable declaration?

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.

Is it possible to combine types in TS?

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.

How do you declare a type in Teamspeak?

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.


1 Answers

Exactly as you typed.

type Foo = {};
type Bar = {};
type Ba = {};
declare type Foob = Foo|Bar|Ba;
let x:Foob;
like image 179
basarat Avatar answered Nov 10 '22 08:11

basarat