microsoft / microsoft/TypeScript

__metadata should register function that returns type instead of literal type

Open
#19,563 7 comments 7 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Domain: Decorators Revisit Suggestion
Dominant language
Go
Stars
111k
Forks
14.3k
Avg merge
2d 4h
Merged PRs (30d)
132

Description

Imagine case with circular dependencies

class Car {
  @Field owner: Person // !!! Error: Person is not defined.
}

class Person {
  @Field car: Car;
}

// car has owner, owner has car

Typescript metadata would be emitted here like

eg.
__metadata('design:type', Person).

As Person is injected for the first time before Person class is initialized, it will result with ReferenceError saying Person is not defined.

If it'd emit metadata like:
__metadata('design:type', () => Person)

it'd be fine.

Later on, when using Reflect.metadata, it would also need to call meta function instead of just returning the type.

If you think it's good idea, do you have any suggestions about starting point for PR that would implement this change?

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

Start by tracing the TypeScript compiler path that emits decorator metadata, then inspect how Reflect.metadata consumes the emitted value. Done means the circular Car/Person example avoids the initialization ReferenceError and metadata consumers invoke the deferred type function; the payload names no files or tests.

Written by the indexing model from the issue text.

Assessment

Tech stack
typescript
Domain
compilers
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.