Skip to content
Tutorials Center
My learning

Topics

TypeScript Reference ยท tsconfig: Language, Modules and Emit Options

TypeScript emitDecoratorMetadata option

Reference for emitDecoratorMetadata: the design:type, design:paramtypes and design:returntype metadata, reflect-metadata, and why interfaces become Object.

emitDecoratorMetadata makes the compiler record the declared types of decorated things as ordinary run-time values. For a decorated property, method or constructor it emits calls such as Reflect.metadata("design:type", String). A library can then read what type a property or parameter has and act on it, which is how dependency-injection containers know which class to pass to a constructor.

It only works together with experimentalDecorators, and it needs a polyfill that supplies Reflect.metadata and Reflect.getMetadata, usually the reflect-metadata package. It does nothing for standard decorators. Because types are erased at run time, only types that exist as values (classes and the built-in constructors) survive.

JSON
{
  "compilerOptions": {
    "experimentalDecorators": true,
    "emitDecoratorMetadata": true
  }
}

Try it

TypeScript What the emitted metadata looks like

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or readFileSync(0)), it gets the next line from this box.

TypeScript A tiny injector built on that metadata

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or readFileSync(0)), it gets the next line from this box.

Syntax

JSON
{ "compilerOptions": { "emitDecoratorMetadata": true } }
Part Description
Value true or false. Default: false. Requires experimentalDecorators.
design:type Emitted for a decorated property, accessor or method: the declared type.
design:paramtypes Emitted for a decorated class or method: the parameter types as an array.
design:returntype Emitted for a decorated method: the return type.

How types map: string, number, boolean and symbol become String, Number, Boolean, Symbol; classes become themselves; arrays and tuples become Array; functions become Function. Everything else, such as interfaces, most unions, any and type parameters, becomes Object.

Usage notes

  • Interfaces do not work as injection tokens. The metadata for a parameter typed with an interface is Object, so containers use a class or an explicit token instead.
  • Imports must stay value imports. A class used only in a decorated signature is still needed at run time for the metadata, so it must not be imported with import type. With isolatedModules the compiler can ask you to change such imports.
  • Output size. Every decorated member gets extra calls; metadata for large codebases adds noticeable bundle weight.
  • Circular imports can make a type undefined when the decorator runs, which shows up as a confusing injection error at start-up.
  • Not an alternative to validation. Metadata describes declared types only; it never checks incoming data.
  • Modern code usually avoids this option, using explicit tokens or factory functions with standard decorators.