microsoft / microsoft/TypeScript

__decorate helper should not use `this` when targeting modules

Open
#35,802 0 comments 2 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Awaiting More Feedback Suggestion
Dominant language
Go
Stars
111k
Forks
14.3k
Avg merge
2d 4h
Merged PRs (30d)
132

Description

TypeScript Version: 3.7.x-dev.201xxxxx

Search Terms: __decorate this module

Code

class Foo {
  @property() x = 1;
}

Expected behavior:
Top-level this reference is not emitted.

Actual behavior:

The __decrate variable is declared like:

var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {

Since top-level this is always undefined in modules, (this && this.__decorate) || can be omitted. It's only a few bytes, but the presence of top-level this also causes warnings in other downstream tools like Rollup.

A few other things could be fixed for an ES6+ helper, btw:

  • Use const instead of var (allows for some VM optimizations)
  • Remove Reflect.decorate
  • Don't use arguments

Playground Link: https://www.typescriptlang.org/play/?ts=Nightly#code/MYGwhgzhAEBiD29oG8BQ1oAEAOAne2AprgC4CeAFAJTQAe0AvNAIwDcqAvkA

Related Issues: None?

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Reproduce the example from the linked TypeScript Playground using the reported 3.7.x-dev version or current compiler, and inspect the emitted __decorate helper. Trace the compiler entry point that emits this helper; done means module output no longer includes the top-level this reference while decorator behavior remains intact.

Written by the indexing model from the issue text.

Assessment

Tech stack
javascript, typescript
Domain
compilers
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
42/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.