# TypeScript 5.0 and the decorators that are finally the real ones

> The decorators in your NestJS codebase are a different feature from the ones TypeScript 5.0 shipped, and which one you use decides whether you can delete a compiler flag.

- Author: Deyan Peev (https://deyanpeev.com)
- Published: 2023-03-25
- Tags: TypeScript, JavaScript, NestJS, Node.js
- URL: https://deyanpeev.com/posts/typescript-five-and-the-real-decorators

TypeScript 5.0 was released on the sixteenth of March, and the headline is
decorators. If you write NestJS you've been using decorators daily for years,
so the natural reaction is to wonder what was missing.

They're a different feature, and which of the two you have decides whether you
can delete a compiler flag.

# Two features with one syntax

What NestJS uses (and Angular, and TypeORM, and every library in that lineage)
are **experimental decorators**, enabled with `experimentalDecorators` in
`tsconfig.json`. They implement a TC39 proposal from around 2015 that never
advanced. TypeScript shipped them anyway because the frameworks needed something,
and the ecosystem built a decade of tooling on a design that was never finished.

What 5.0 shipped is the **Stage 3 proposal**, the one that's on track to become
part of JavaScript itself. Same `@` syntax, different semantics, and the
function you write looks different too.

Under the old design a method decorator receives the target, the property key and
the property descriptor. Under the new one it receives the value and a context
object:

```ts
function logged<This, Args extends any[], Return>(
  target: (this: This, ...args: Args) => Return,
  context: ClassMethodDecoratorContext<This, (this: This, ...args: Args) => Return>,
) {
  return function (this: This, ...args: Args): Return {
    console.log(`entering ${String(context.name)}`)
    return target.call(this, ...args)
  }
}

class Service {
  @logged
  handle(id: string) {
    return id.toUpperCase()
  }
}
```

The `context` object is the improvement, I think. It carries the name, the kind,
whether the member is static or private, and an `addInitializer` hook. And it's
typed, which the old descriptor-based signature never usefully was.

# The flags stay on for NestJS

The two can't be mixed. If `experimentalDecorators` is on you get the old
behaviour, turn it off and you get the new one.

There is also no metadata. `emitDecoratorMetadata` (the flag that makes
TypeScript emit design-time type information for `reflect-metadata` to read) is
tied to the experimental implementation. That flag is how NestJS resolves a
constructor parameter to a provider without you naming it, and how class-validator
knows a field is a `Date`. The standard decorators have no equivalent in 5.0.

So for anything built on that ecosystem, nothing changes this March. Keep both
flags on, keep writing decorators the way the framework documents, and wait for
the frameworks to move. They will, and it won't be a small release when they
do.

The new ones are useful right away in your own code, a `@logged`, a
`@measured`, a `@retry` in a service you own, as long as the project does not
depend on decorator metadata. There the standard version is better typed and
will eventually need no compiler support at all.

# The rest of the release, which I adopted the same week

Two smaller things I've already used.

`const` type parameters, which fix an annoyance that has made me write `as const`
thousands of times:

```ts
function tags<const T extends readonly string[]>(values: T): T {
  return values
}

const t = tags(['aws', 'terraform']) // readonly ["aws", "terraform"]
```

Previously that inferred `string[]` and you had to remember the assertion at every
call site. Now the library author makes the decision once.

And all enums are union enums now, so a numeric enum finally narrows properly
instead of accepting any number that happens to fit.

The module resolution work is the other one I'd look at. `moduleResolution:
bundler` describes what bundlers do, where before you had to pick between two
Node resolution modes that both describe something else. If you've ever argued
with a `tsconfig` about file extensions in import paths, that flag is probably
what you wanted.

# How it got here

TypeScript shipped a feature early, the standards process took eight more years,
and now there are two incompatible things with the same syntax in the same
language. I don't think anyone did anything unreasonable at any step. The
frameworks needed decorators and couldn't wait, and TC39 was right to keep
working.

When I adopt something labelled experimental, I try to write down what I'll do
when the real version arrives and does not match.
