10 enums namespaces symbols

These are the TypeScript features that emit runtime code Know which ones you are actually using

Table of Contents

Enums

enum Protocol {
  TCP = "tcp",
  UDP = "udp",
}

A string enum emits an object at runtime Protocol.TCP is "tcp" A plain union of literals often does the same job with no emitted object Use enums when you need the runtime object

Namespaces

namespace Utils {
  export const label = "utils";
}

Namespaces emit an object too Modern code usually prefers modules The handbook keeps them for specific compatibility cases

Declaration merging

Interfaces with the same name in the same scope merge into one Classes and namespaces can merge This is how some libraries attach types to a runtime value It is also how you get confusing duplicate declarations by accident

Symbols

const marker: unique symbol = Symbol("marker");

unique symbol is a type only for const declarations It gives you a key that cannot collide with a string key Useful for metadata on objects

Posted