Syntax

TypeScript Union Type Syntax

In TypeScript, union type: allows a value to be one of several types — let id: string | number;

Quick Answer

Syntax
let id: string | number;
TypeScriptSyntax

Union Type

Code example
let id: string | number;

Allows a value to be one of several types.

Learn More

How to union type in TypeScript

  1. Open your TypeScript file or editor.
  2. Copy the example, then replace its sample names and values for your code.

TypeScript Union Type Syntax

Use let id: string | number;.

Related Syntax References

Frequently asked questions

What is the TypeScript syntax for union type?

Use this pattern: let id: string | number;

Can I copy this TypeScript code example?

Yes. Use Copy code and adapt the example to your program.

Is this TypeScript syntax different on Mac or Windows?

No. The language syntax is the same across operating systems.

Official source · How we verify