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;
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.
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.
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 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.
Yeah, just needs the correct syntax for a type alias:
export type TypeAB = TypeA | TypeB;
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