Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Export a union type alias in typescript

Tags:

typescript

Is it possible to export something like this:

export TypeA | TypeB as TypeAB;

and declare a variable of TypeAB that can be either of TypeA or TypeB:

import {TypeAB} from './typeab';
var ab: TypeAB;
like image 500
Marius Avatar asked May 03 '16 14:05

Marius


People also ask

Can you export a type in TypeScript?

Use a named export to export a type in TypeScript, e.g. export type Person = {} . The exported type can be imported by using a named import as import {Person} from './another-file' . You can have as many named exports as necessary in a single file.

How do you handle a union type in TypeScript?

TypeScript Union Type Narrowing To narrow a variable to a specific type, implement a type guard. Use the typeof operator with the variable name and compare it with the type you expect for the variable.

What is type alias in TypeScript?

In Typescript, Type aliases give a type a new name. They are similar to interfaces in that they can be used to name primitives and any other kinds that you'd have to define by hand otherwise. Aliasing doesn't truly create a new type; instead, it gives that type a new name.

How do you make a union type from a type alias or interface properties in TypeScript?

How to make a union type from a type alias or interface properties in TypeScript? To make a union type from a type alias or interface properties, you can use the indexed access type syntax and then write the property names to make the union type separated by the union operator symbol ( | ) in TypeScript.


1 Answers

Yeah, just needs the correct syntax for a type alias:

export type TypeAB = TypeA | TypeB;
like image 52
David Sherret Avatar answered Oct 15 '22 16:10

David Sherret