1 | # Metadata Reflection API
|
---|
2 |
|
---|
3 | * [Detailed proposal][Metadata-Spec]
|
---|
4 |
|
---|
5 | ## Installation
|
---|
6 |
|
---|
7 | ```
|
---|
8 | npm install reflect-metadata
|
---|
9 | ```
|
---|
10 |
|
---|
11 | ## Background
|
---|
12 |
|
---|
13 | * Decorators add the ability to augment a class and its members as the class is defined, through a declarative syntax.
|
---|
14 | * Traceur attaches annotations to a static property on the class.
|
---|
15 | * Languages like C# (.NET), and Java support attributes or annotations that add metadata to types, along with a reflective API for reading metadata.
|
---|
16 |
|
---|
17 | ## Goals
|
---|
18 |
|
---|
19 | * A number of use cases (Composition/Dependency Injection, Runtime Type Assertions, Reflection/Mirroring, Testing) want the ability to add additional metadata to a class in a consistent manner.
|
---|
20 | * A consistent approach is needed for various tools and libraries to be able to reason over metadata.
|
---|
21 | * Metadata-producing decorators (nee. "Annotations") need to be generally composable with mutating decorators.
|
---|
22 | * Metadata should be available not only on an object but also through a Proxy, with related traps.
|
---|
23 | * Defining new metadata-producing decorators should not be arduous or over-complex for a developer.
|
---|
24 | * Metadata should be consistent with other language and runtime features of ECMAScript.
|
---|
25 |
|
---|
26 | ## Syntax
|
---|
27 |
|
---|
28 | * Declarative definition of metadata:
|
---|
29 | ```JavaScript
|
---|
30 | class C {
|
---|
31 | @Reflect.metadata(metadataKey, metadataValue)
|
---|
32 | method() {
|
---|
33 | }
|
---|
34 | }
|
---|
35 | ```
|
---|
36 |
|
---|
37 | * Imperative definition of metadata:
|
---|
38 | ```JavaScript
|
---|
39 | Reflect.defineMetadata(metadataKey, metadataValue, C.prototype, "method");
|
---|
40 | ```
|
---|
41 |
|
---|
42 | * Imperative introspection of metadata:
|
---|
43 | ```JavaScript
|
---|
44 | let obj = new C();
|
---|
45 | let metadataValue = Reflect.getMetadata(metadataKey, obj, "method");
|
---|
46 | ```
|
---|
47 |
|
---|
48 | ## Semantics
|
---|
49 |
|
---|
50 | * Object has a new \[\[Metadata\]\] internal property that will contain a Map whose keys are property keys (or **undefined**) and whose values are Maps of metadata keys to metadata values.
|
---|
51 | * Object will have a number of new internal methods for \[\[DefineOwnMetadata\]\], \[\[GetOwnMetadata\]\], \[\[HasOwnMetadata\]\], etc.
|
---|
52 | * These internal methods can be overridden by a Proxy to support additional traps.
|
---|
53 | * These internal methods will by default call a set of abstract operations to define and read metadata.
|
---|
54 | * The Reflect object will expose the MOP operations to allow imperative access to metadata.
|
---|
55 | * Metadata defined on class declaration *C* is stored in *C*.\[\[Metadata\]\], with **undefined** as the key.
|
---|
56 | * Metadata defined on static members of class declaration *C* are stored in *C*.\[\[Metadata\]\], with the property key as the key.
|
---|
57 | * Metadata defined on instance members of class declaration *C* are stored in *C*.prototype.\[\[Metadata\]\], with the property key as the key.
|
---|
58 |
|
---|
59 | ## API
|
---|
60 |
|
---|
61 | ```JavaScript
|
---|
62 | // define metadata on an object or property
|
---|
63 | Reflect.defineMetadata(metadataKey, metadataValue, target);
|
---|
64 | Reflect.defineMetadata(metadataKey, metadataValue, target, propertyKey);
|
---|
65 |
|
---|
66 | // check for presence of a metadata key on the prototype chain of an object or property
|
---|
67 | let result = Reflect.hasMetadata(metadataKey, target);
|
---|
68 | let result = Reflect.hasMetadata(metadataKey, target, propertyKey);
|
---|
69 |
|
---|
70 | // check for presence of an own metadata key of an object or property
|
---|
71 | let result = Reflect.hasOwnMetadata(metadataKey, target);
|
---|
72 | let result = Reflect.hasOwnMetadata(metadataKey, target, propertyKey);
|
---|
73 |
|
---|
74 | // get metadata value of a metadata key on the prototype chain of an object or property
|
---|
75 | let result = Reflect.getMetadata(metadataKey, target);
|
---|
76 | let result = Reflect.getMetadata(metadataKey, target, propertyKey);
|
---|
77 |
|
---|
78 | // get metadata value of an own metadata key of an object or property
|
---|
79 | let result = Reflect.getOwnMetadata(metadataKey, target);
|
---|
80 | let result = Reflect.getOwnMetadata(metadataKey, target, propertyKey);
|
---|
81 |
|
---|
82 | // get all metadata keys on the prototype chain of an object or property
|
---|
83 | let result = Reflect.getMetadataKeys(target);
|
---|
84 | let result = Reflect.getMetadataKeys(target, propertyKey);
|
---|
85 |
|
---|
86 | // get all own metadata keys of an object or property
|
---|
87 | let result = Reflect.getOwnMetadataKeys(target);
|
---|
88 | let result = Reflect.getOwnMetadataKeys(target, propertyKey);
|
---|
89 |
|
---|
90 | // delete metadata from an object or property
|
---|
91 | let result = Reflect.deleteMetadata(metadataKey, target);
|
---|
92 | let result = Reflect.deleteMetadata(metadataKey, target, propertyKey);
|
---|
93 |
|
---|
94 | // apply metadata via a decorator to a constructor
|
---|
95 | @Reflect.metadata(metadataKey, metadataValue)
|
---|
96 | class C {
|
---|
97 | // apply metadata via a decorator to a method (property)
|
---|
98 | @Reflect.metadata(metadataKey, metadataValue)
|
---|
99 | method() {
|
---|
100 | }
|
---|
101 | }
|
---|
102 | ```
|
---|
103 |
|
---|
104 | ## Alternatives
|
---|
105 |
|
---|
106 | * Use properties rather than a separate API.
|
---|
107 | * Obvious downside is that this can be a lot of code:
|
---|
108 | ```JavaScript
|
---|
109 | function ParamTypes(...types) {
|
---|
110 | return (target, propertyKey) => {
|
---|
111 | const symParamTypes = Symbol.for("design:paramtypes");
|
---|
112 | if (propertyKey === undefined) {
|
---|
113 | target[symParamTypes] = types;
|
---|
114 | }
|
---|
115 | else {
|
---|
116 | const symProperties = Symbol.for("design:properties");
|
---|
117 | let properties, property;
|
---|
118 | if (Object.prototype.hasOwnProperty.call(target, symProperties)) {
|
---|
119 | properties = target[symProperties];
|
---|
120 | }
|
---|
121 | else {
|
---|
122 | properties = target[symProperties] = {};
|
---|
123 | }
|
---|
124 | if (Object.prototype.hasOwnProperty.call(properties, propertyKey)) {
|
---|
125 | property = properties[propertyKey];
|
---|
126 | }
|
---|
127 | else {
|
---|
128 | property = properties[propertyKey] = {};
|
---|
129 | }
|
---|
130 | property[symParamTypes] = types;
|
---|
131 | }
|
---|
132 | };
|
---|
133 | }
|
---|
134 | ```
|
---|
135 |
|
---|
136 | ## Notes
|
---|
137 | * Though it may seem counterintuitive, the methods on Reflect place the parameters for the metadata key and metadata value before the target or property key. This is due to the fact that the property key is the only optional parameter in the argument list. This also makes the methods easier to curry with Function#bind. This also helps reduce the overall footprint and complexity of a metadata-producing decorator that could target both a class or a property:
|
---|
138 |
|
---|
139 | ```JavaScript
|
---|
140 | function ParamTypes(...types) {
|
---|
141 | // as propertyKey is effectively optional, its easier to use here
|
---|
142 | return (target, propertyKey) => { Reflect.defineMetadata("design:paramtypes", types, target, propertyKey); }
|
---|
143 |
|
---|
144 | // vs. having multiple overloads with the target and key in the front:
|
---|
145 | //
|
---|
146 | // return (target, propertyKey) => {
|
---|
147 | // if (propertyKey === undefined) {
|
---|
148 | // Reflect.defineMetadata(target, "design:paramtypes", types);
|
---|
149 | // }
|
---|
150 | // else {
|
---|
151 | // Reflect.defineMetadata(target, propertyKey, "design:paramtypes", types);
|
---|
152 | // }
|
---|
153 | // }
|
---|
154 | //
|
---|
155 | // vs. having a different methods for the class or a property:
|
---|
156 | //
|
---|
157 | // return (target, propertyKey) => {
|
---|
158 | // if (propertyKey === undefined) {
|
---|
159 | // Reflect.defineMetadata(target, "design:paramtypes", types);
|
---|
160 | // }
|
---|
161 | // else {
|
---|
162 | // Reflect.definePropertyMetadata(target, propertyKey, "design:paramtypes", types);
|
---|
163 | // }
|
---|
164 | // }
|
---|
165 | }
|
---|
166 | ```
|
---|
167 |
|
---|
168 | * To enable experimental support for metadata decorators in your TypeScript project, you must add `"experimentalDecorators": true` to your tsconfig.json file.
|
---|
169 | * To enable experimental support for auto-generated type metadata in your TypeScript project, you must add `"emitDecoratorMetadata": true` to your tsconfig.json file.
|
---|
170 | * Please note that auto-generated type metadata may have issues with circular or forward references for types.
|
---|
171 |
|
---|
172 | ## Issues
|
---|
173 |
|
---|
174 | * A poorly written mutating decorator for a class constructor could cause metadata to become lost if the prototype chain is not maintained. Though, not maintaining the prototype chain in a mutating decorator for a class constructor would have other negative side effects as well. @rbuckton
|
---|
175 | * This is mitigated if the mutating decorator returns a class expression that extends from the target, or returns a proxy for the decorator. @rbuckton
|
---|
176 | * Metadata for a method is attached to the class (or prototype) via the property key. It would not then be available if trying to read metadata on the function of the method (e.g. "tearing-off" the method from the class). @rbuckton
|
---|
177 |
|
---|
178 | [Metadata-Spec]: https://rbuckton.github.io/reflect-metadata |
---|