docs(dependency-injection): Clarify how TypeScript generates decorator metadata
closes #1960
This commit is contained in:
parent
ce8b94799c
commit
fc1a9a6026
|
@ -430,11 +430,20 @@ block injectable-not-always-needed-in-ts
|
|||
+ifDocsFor('ts')
|
||||
.l-sub-section
|
||||
:marked
|
||||
Injectors use a class's constructor metadata to determine dependent types as
|
||||
identified by the constructor's parameter types.
|
||||
TypeScript generates such metadata for any class with a decorator, and any decorator will do.
|
||||
But of course, it is more meaningful to mark a class using the appropriate
|
||||
<a href="#{injMetaUrl}">InjectableMetadata</a> #{_decorator}.
|
||||
At runtime, injectors can read class metadata in the transpiled JavaScript code
|
||||
and use the constructor parameter type information
|
||||
to determine what things to inject.
|
||||
|
||||
Not every JavaScript class has metadata.
|
||||
The TypeScript compiler discards metadata by default.
|
||||
If the `emitDecoratorMetadata` compiler option is true
|
||||
(as it should be in the `tsconfig.json`),
|
||||
the compiler adds the metadata to the generated JavaScript
|
||||
for _every class with at least one decorator_.
|
||||
|
||||
While any decorator will trigger this effect, mark the service class with the
|
||||
<a href="#{injMetaUrl}">InjectableMetadata</a> #{_decorator}
|
||||
to make the intent clear.
|
||||
|
||||
.callout.is-critical
|
||||
header Always include the parentheses
|
||||
|
|
Loading…
Reference in New Issue