microsoft / TypeScript

TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
https://www.typescriptlang.org
Apache License 2.0
100.85k stars 12.46k forks source link

__metadata should register function that returns type instead of literal type #19563

Open pie6k opened 7 years ago

pie6k commented 7 years ago

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?

mhegazy commented 6 years ago

This is already referenced in https://github.com/Microsoft/TypeScript/issues/14971

mhegazy commented 6 years ago

We plan on revisiting some of the decorator decisions as we implement the new decorator proposal

pie6k commented 6 years ago

@mhegazy I understand it might be hard to answer - but do you have any, even rough estamine on that? It's something thats quite urgent for my project :)

mhegazy commented 6 years ago

I do not have an ETA at the moment.

bolu commented 6 years ago

Hi, is there any update on the ETA of this? I can imagine some simple changes that would make things work. For example, emit all the decorators at end of file. Or after last class defined in the file.

keatkeat87 commented 6 years ago

today i change my tsconfig target to es2015, this problem come out. Error: SomeClass is not defined any update or alternative work around ? and why target es5 work fine ?

BeSpunky commented 4 years ago

Any developments? I'm stuck with the same issue...