microsoft / microsoft/TypeScript
Decorator Metdata: expose array element type
Nobody has claimed this yet.
- Dominant language
- Go
- Stars
- 111k
- Forks
- 14.3k
- Avg merge
- 2d 4h
- Merged PRs (30d)
- 132
Description
TSC 175, target ES6, Node 5.6
Sample class with decorated properties:
@sg.DMClass
export class User implements IUser {
@sg.DMProperty
firstName: string;
@sg.DMProperty
lastName: string;
@sg.DMProperty
friends: User[];
}
Property decorator:
export function DMProperty(target: any, key: string) {
var type = Reflect.getMetadata("design:type", target, key);
}
Current behaviour:
When the decorator gets called for property "friends", "type" is "Array" and the captured type ("User") is lost.
Expected behaviour
"type" should contain the captured type ("User"), too
Following very dirty work-around does the job: I modified tsc's method "emitSerializedTypeNode":
function emitSerializedTypeNode(node) {
if (node) {
console.log("*** emitSerializedTypeNode: node.kind: ", node.kind);
switch (node.kind) {
...
case 157:
// Begin dirty ---------------------------------------------------------------------------
var _elemType = node.elementType.typeName.text;
write("{name: 'Array<" + _elemType + ">', type:'Array', elemType:'" + _elemType + "'}");
console.log("*** emitSerializedTypeNode: Array, type: ", node.elementType.typeName.text);
// End dirty -----------------------------------------------------------------------------
return;
case 150:
...
Best
Mind
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start at the TypeScript compiler's emitSerializedTypeNode method, especially its handling of array nodes. Compare the current design:type metadata for User[] with the issue's expected element-type information and the proposed workaround. Done means emitted metadata preserves the array element type while retaining the existing Array type information.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- javascript, typescript
- Domain
- compilers
- Issue type
- Feature
- Difficulty
- 5/5
- Estimated time
- Over a week
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 25/100