{"componentChunkName":"component---src-templates-handbook-tsx","path":"/docs/handbook/decorators.html","result":{"data":{"allSitePage":{"nodes":[{"path":"/dev-404-page/"},{"path":"/docs/handbook/advanced-types.html"},{"path":"/docs/handbook/basic-types.html"},{"path":"/docs/handbook/classes.html"},{"path":"/docs/handbook/compiler-options-in-msbuild.html"},{"path":"/docs/handbook/compiler-options.html"},{"path":"/docs/handbook/configuring-watch.html"},{"path":"/docs/handbook/declaration-merging.html"},{"path":"/docs/handbook/decorators.html"},{"path":"/docs/handbook/enums.html"},{"path":"/docs/handbook/functions.html"},{"path":"/docs/handbook/generics.html"},{"path":"/docs/handbook/integrating-with-build-tools.html"},{"path":"/docs/handbook/interfaces.html"},{"path":"/docs/handbook/iterators-and-generators.html"},{"path":"/docs/handbook/jsdoc-supported-types.html"},{"path":"/docs/handbook/jsx.html"},{"path":"/docs/handbook/literal-types.html"},{"path":"/docs/handbook/mixins.html"},{"path":"/docs/handbook/module-resolution.html"},{"path":"/docs/handbook/modules.html"},{"path":"/docs/handbook/namespaces-and-modules.html"},{"path":"/docs/handbook/namespaces.html"},{"path":"/docs/handbook/nightly-builds.html"},{"path":"/docs/handbook/project-references.html"},{"path":"/docs/handbook/symbols.html"},{"path":"/docs/handbook/"},{"path":"/docs/handbook/triple-slash-directives.html"},{"path":"/docs/handbook/type-checking-javascript-files.html"},{"path":"/docs/handbook/type-compatibility.html"},{"path":"/docs/handbook/type-inference.html"},{"path":"/docs/handbook/unions-and-intersections.html"},{"path":"/docs/handbook/utility-types.html"},{"path":"/docs/handbook/variable-declarations.html"},{"path":"/docs/handbook/writing-declaration-files.html"},{"path":"/docs/handbook/declaration-files/by-example.html"},{"path":"/docs/handbook/declaration-files/consumption.html"},{"path":"/docs/handbook/declaration-files/deep-dive.html"},{"path":"/docs/handbook/declaration-files/do-s-and-don-ts.html"},{"path":"/docs/handbook/declaration-files/introduction.html"},{"path":"/docs/handbook/declaration-files/library-structures.html"},{"path":"/docs/handbook/declaration-files/publishing.html"},{"path":"/docs/handbook/declaration-files/templates.html"},{"path":"/docs/handbook/release-notes/typescript-1-1.html"},{"path":"/docs/handbook/release-notes/typescript-1-3.html"},{"path":"/docs/handbook/release-notes/typescript-1-4.html"},{"path":"/docs/handbook/release-notes/typescript-1-5.html"},{"path":"/docs/handbook/release-notes/typescript-1-6.html"},{"path":"/docs/handbook/release-notes/typescript-1-7.html"},{"path":"/docs/handbook/release-notes/typescript-1-8.html"},{"path":"/docs/handbook/release-notes/typescript-2-0.html"},{"path":"/docs/handbook/release-notes/typescript-2-2.html"},{"path":"/docs/handbook/release-notes/typescript-2-1.html"},{"path":"/docs/handbook/release-notes/typescript-2-3.html"},{"path":"/docs/handbook/release-notes/typescript-2-4.html"},{"path":"/docs/handbook/release-notes/typescript-2-5.html"},{"path":"/docs/handbook/release-notes/typescript-2-6.html"},{"path":"/docs/handbook/release-notes/typescript-2-7.html"},{"path":"/docs/handbook/release-notes/typescript-2-8.html"},{"path":"/docs/handbook/release-notes/typescript-2-9.html"},{"path":"/docs/handbook/release-notes/typescript-3-0.html"},{"path":"/docs/handbook/release-notes/typescript-3-1.html"},{"path":"/docs/handbook/release-notes/typescript-3-2.html"},{"path":"/docs/handbook/release-notes/typescript-3-3.html"},{"path":"/docs/handbook/release-notes/typescript-3-4.html"},{"path":"/docs/handbook/release-notes/typescript-3-5.html"},{"path":"/docs/handbook/release-notes/typescript-3-6.html"},{"path":"/docs/handbook/release-notes/typescript-3-7.html"},{"path":"/docs/handbook/release-notes/typescript-3-8.html"},{"path":"/docs/handbook/asp-net-core.html"},{"path":"/docs/handbook/angular.html"},{"path":"/docs/handbook/dom-manipulation.html"},{"path":"/docs/handbook/gulp.html"},{"path":"/docs/handbook/migrating-from-javascript.html"},{"path":"/docs/handbook/react-&-webpack.html"},{"path":"/docs/handbook/react.html"},{"path":"/docs/handbook/typescript-in-5-minutes-func.html"},{"path":"/docs/handbook/typescript-in-5-minutes.html"},{"path":"/docs/handbook/typescript-in-5-minutes-oop.html"},{"path":"/docs/handbook/typescript-from-scratch.html"},{"path":"/docs/handbook/typescript-tooling-in-5-minutes.html"},{"path":"/docs/handbook/tsconfig-json.html"},{"path":"/docs/handbook/declaration-files/templates/global-modifying-module-d-ts.html"},{"path":"/docs/handbook/declaration-files/templates/global-plugin-d-ts.html"},{"path":"/docs/handbook/declaration-files/templates/global-d-ts.html"},{"path":"/docs/handbook/declaration-files/templates/module-class-d-ts.html"},{"path":"/docs/handbook/declaration-files/templates/module-function-d-ts.html"},{"path":"/docs/handbook/declaration-files/templates/module-plugin-d-ts.html"},{"path":"/docs/handbook/declaration-files/templates/module-d-ts.html"},{"path":"/docs/handbook/release-notes/overview.html"},{"path":"/tsconfig"},{"path":"/en/tsconfig"},{"path":"/ja/tsconfig"},{"path":"/vo/tsconfig"},{"path":"/play"},{"path":"/en/play"},{"path":"/es/play"},{"path":"/vo/play"},{"path":"/zh/play"},{"path":"/ja/play"},{"path":"/play/3-7/fixits/big-number-literals.ts"},{"path":"/play/3-7/fixits/const-to-let.ts"},{"path":"/play/3-7/fixits/infer-from-usage-changes.ts"},{"path":"/play/3-7/syntax-and-messaging/flattened-error-reporting.ts"},{"path":"/play/3-7/syntax-and-messaging/nullish-coalescing.ts"},{"path":"/play/3-7/syntax-and-messaging/optional-chaining.ts"},{"path":"/play/3-7/types-and-code-flow/assertion-functions.ts"},{"path":"/play/3-7/types-and-code-flow/recursive-type-references.ts"},{"path":"/play/3-7/types-and-code-flow/uncalled-function-checks.ts"},{"path":"/play/3-8/breaking-changes/checking-unions-with-index-signatures.ts"},{"path":"/play/3-8/jsdoc-improvements/accessibility-modifiers.js"},{"path":"/play/3-8/syntax-and-messaging/export-modules-from.ts"},{"path":"/play/3-8/syntax-and-messaging/private-class-fields.ts"},{"path":"/play/javascript/external-apis/typescript-with-deno.ts"},{"path":"/play/javascript/external-apis/typescript-with-node.js"},{"path":"/play/javascript/external-apis/typescript-with-web.js"},{"path":"/play/javascript/external-apis/typescript-with-webgl.js"},{"path":"/play/javascript/functions-with-javascript/function-chaining.ts"},{"path":"/play/javascript/functions-with-javascript/generic-functions.ts"},{"path":"/play/javascript/functions-with-javascript/typing-functions.ts"},{"path":"/play/javascript/helping-with-javascript/errors.ts"},{"path":"/play/javascript/helping-with-javascript/quick-fixes.ts"},{"path":"/play/javascript/javascript-essentials/code-flow.ts"},{"path":"/play/javascript/javascript-essentials/functions.ts"},{"path":"/play/javascript/javascript-essentials/hello-world.ts"},{"path":"/play/javascript/javascript-essentials/objects-and-arrays.ts"},{"path":"/play/javascript/modern-javascript/async-await.ts"},{"path":"/play/javascript/modern-javascript/immutability.ts"},{"path":"/play/javascript/modern-javascript/import-export.ts"},{"path":"/play/javascript/modern-javascript/jsdoc-support.js"},{"path":"/play/javascript/working-with-classes/classes-101.ts"},{"path":"/play/javascript/working-with-classes/generic-classes.ts"},{"path":"/play/javascript/working-with-classes/mixins.ts"},{"path":"/play/javascript/working-with-classes/this.ts"},{"path":"/play/playground/config/javascript-playgrounds.js"},{"path":"/play/playground/config/new-compiler-defaults.ts"},{"path":"/play/playground/language/automatic-type-acquisition.ts"},{"path":"/play/playground/language/fixits.ts"},{"path":"/play/playground/tooling/mobile-support.ts"},{"path":"/play/playground/tooling/sharable-urls.ts"},{"path":"/play/playground/tooling/typescript-versions.ts"},{"path":"/play/typescript/language/soundness.ts"},{"path":"/play/typescript/language/structural-typing.ts"},{"path":"/play/typescript/language/type-guards.ts"},{"path":"/play/typescript/language/type-widening-and-narrowing.ts"},{"path":"/play/typescript/language-extensions/enums.ts"},{"path":"/play/typescript/language-extensions/nominal-typing.ts"},{"path":"/play/typescript/language-extensions/types-vs-interfaces.ts"},{"path":"/play/typescript/meta-types/conditional-types.ts"},{"path":"/play/typescript/meta-types/discriminate-types.ts"},{"path":"/play/typescript/meta-types/indexed-types.ts"},{"path":"/play/typescript/meta-types/mapped-types.ts"},{"path":"/play/typescript/primitives/any.ts"},{"path":"/play/typescript/primitives/literals.ts"},{"path":"/play/typescript/primitives/union-and-intersection-types.ts"},{"path":"/play/typescript/primitives/unknown-and-never.ts"},{"path":"/play/typescript/type-primitives/built-in-utility-types.ts"},{"path":"/play/typescript/type-primitives/nullable-types.ts"},{"path":"/play/typescript/type-primitives/tuples.ts"},{"path":"/es/play/javascript/external-apis/typescript-with-deno.ts"},{"path":"/es/play/javascript/external-apis/typescript-with-node.js"},{"path":"/es/play/javascript/external-apis/typescript-with-web.js"},{"path":"/es/play/javascript/functions-with-javascript/function-chaining.ts"},{"path":"/es/play/javascript/functions-with-javascript/generic-functions.ts"},{"path":"/es/play/javascript/javascript-essentials/hello-world.ts"},{"path":"/es/play/javascript/javascript-essentials/objects-and-arrays.ts"},{"path":"/es/play/typescript/type-primitives/built-in-utility-types.ts"},{"path":"/es/play/typescript/type-primitives/nullable-types.ts"},{"path":"/es/play/typescript/type-primitives/tuples.ts"},{"path":"/ja/play/typescript/language-extensions/enums.ts"},{"path":"/ja/play/typescript/language-extensions/nominal-typing.ts"},{"path":"/ja/play/typescript/language-extensions/types-vs-interfaces.ts"},{"path":"/ja/play/typescript/primitives/any.ts"},{"path":"/ja/play/typescript/primitives/literals.ts"},{"path":"/ja/play/typescript/primitives/union-and-intersection-types.ts"},{"path":"/ja/play/typescript/type-primitives/built-in-utility-types.ts"},{"path":"/vo/play/javascript/javascript-essentials/code-flow.ts"},{"path":"/vo/play/javascript/javascript-essentials/functions.ts"},{"path":"/vo/play/javascript/javascript-essentials/hello-world.ts"},{"path":"/vo/play/javascript/javascript-essentials/objects-and-arrays.ts"},{"path":"/zh/play/javascript/external-apis/typescript-with-deno.ts"},{"path":"/zh/play/javascript/external-apis/typescript-with-node.js"},{"path":"/zh/play/javascript/external-apis/typescript-with-web.js"},{"path":"/zh/play/javascript/external-apis/typescript-with-webgl.js"},{"path":"/zh/play/javascript/functions-with-javascript/function-chaining.ts"},{"path":"/zh/play/javascript/functions-with-javascript/generic-functions.ts"},{"path":"/zh/play/javascript/functions-with-javascript/typing-functions.ts"},{"path":"/zh/play/javascript/helping-with-javascript/errors.ts"},{"path":"/zh/play/javascript/helping-with-javascript/quick-fixes.ts"},{"path":"/zh/play/javascript/javascript-essentials/code-flow.ts"},{"path":"/zh/play/javascript/javascript-essentials/functions.ts"},{"path":"/zh/play/javascript/javascript-essentials/hello-world.ts"},{"path":"/zh/play/javascript/modern-javascript/import-export.ts"},{"path":"/zh/play/javascript/modern-javascript/jsdoc-support.js"},{"path":"/zh/play/javascript/working-with-classes/classes-101.ts"},{"path":"/zh/play/javascript/working-with-classes/generic-classes.ts"},{"path":"/zh/play/javascript/working-with-classes/mixins.ts"},{"path":"/zh/play/javascript/working-with-classes/this.ts"},{"path":"/zh/play/playground/config/javascript-playgrounds.js"},{"path":"/zh/play/playground/config/new-compiler-defaults.ts"},{"path":"/zh/play/playground/language/automatic-type-acquisition.ts"},{"path":"/zh/play/playground/language/fixits.ts"},{"path":"/zh/play/playground/tooling/mobile-support.ts"},{"path":"/zh/play/playground/tooling/sharable-urls.ts"},{"path":"/zh/play/playground/tooling/typescript-versions.ts"},{"path":"/zh/play/typescript/language/soundness.ts"},{"path":"/zh/play/typescript/language/structural-typing.ts"},{"path":"/zh/play/typescript/language/type-guards.ts"},{"path":"/zh/play/typescript/language/type-widening-and-narrowing.ts"},{"path":"/zh/play/typescript/language-extensions/enums.ts"},{"path":"/zh/play/typescript/language-extensions/nominal-typing.ts"},{"path":"/zh/play/typescript/language-extensions/types-vs-interfaces.ts"},{"path":"/zh/play/typescript/meta-types/conditional-types.ts"},{"path":"/zh/play/typescript/meta-types/discriminate-types.ts"},{"path":"/zh/play/typescript/meta-types/indexed-types.ts"},{"path":"/zh/play/typescript/meta-types/mapped-types.ts"},{"path":"/zh/play/typescript/primitives/any.ts"},{"path":"/zh/play/typescript/primitives/literals.ts"},{"path":"/zh/play/typescript/primitives/union-and-intersection-types.ts"},{"path":"/zh/play/typescript/primitives/unknown-and-never.ts"},{"path":"/zh/play/typescript/type-primitives/built-in-utility-types.ts"},{"path":"/zh/play/typescript/type-primitives/nullable-types.ts"},{"path":"/zh/play/typescript/type-primitives/tuples.ts"},{"path":"/zh/play/javascript/modern-javascript/immutability.ts"},{"path":"/zh/play/javascript/modern-javascript/async-await.ts"},{"path":"/zh/play/javascript/javascript-essentials/objects-and-arrays.ts"},{"path":"/community"},{"path":"/es/community"},{"path":"/ja/community"},{"path":"/vo/community"},{"path":"/zh/community"},{"path":"/download"},{"path":"/es/download"},{"path":"/ja/download"},{"path":"/vo/download"},{"path":"/zh/download"},{"path":"/empty"},{"path":"/es/empty"},{"path":"/ja/empty"},{"path":"/vo/empty"},{"path":"/zh/empty"},{"path":"/"},{"path":"/es/"},{"path":"/ja/"},{"path":"/vo/"},{"path":"/zh/"},{"path":"/tools"},{"path":"/es/tools"},{"path":"/ja/tools"},{"path":"/vo/tools"},{"path":"/zh/tools"},{"path":"/why-create-typescript"},{"path":"/es/why-create-typescript"},{"path":"/ja/why-create-typescript"},{"path":"/vo/why-create-typescript"},{"path":"/zh/why-create-typescript"},{"path":"/docs/home"},{"path":"/es/docs/home"},{"path":"/ja/docs/home"},{"path":"/vo/docs/home"},{"path":"/zh/docs/home"},{"path":"/dev/playground-plugins/"},{"path":"/dev/sandbox/"},{"path":"/dev/twoslash/"},{"path":"/dev/typescript-vfs/"}]},"markdownRemark":{"id":"0169e027-3ed6-5ae2-a981-9ea48559c63e","excerpt":"Introduction With the introduction of Classes in TypeScript and ES6, there now exist certain scenarios that require additional features to support annotating or…","html":"<h1 id=\"introduction\" style=\"position:relative;\"><a href=\"#introduction\" aria-label=\"introduction permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Introduction</h1>\n<p>With the introduction of Classes in TypeScript and ES6, there now exist certain scenarios that require additional features to support annotating or modifying classes and class members.\nDecorators provide a way to add both annotations and a meta-programming syntax for class declarations and members.\nDecorators are a <a href=\"https://github.com/tc39/proposal-decorators\">stage 2 proposal</a> for JavaScript and are available as an experimental feature of TypeScript.</p>\n<blockquote>\n<p>NOTE  Decorators are an experimental feature that may change in future releases.</p>\n</blockquote>\n<p>To enable experimental support for decorators, you must enable the <code>experimentalDecorators</code> compiler option either on the command line or in your <code>tsconfig.json</code>:</p>\n<p><strong>Command Line</strong>:</p>\n<pre class=\"shiki\"><div class=\"language-id\">shell</div><div class='code-container'><code><span style=\"color: #000000\">tsc --target ES5 --experimentalDecorators</span></code></div></pre>\n<p><strong>tsconfig.json</strong>:</p>\n<pre class=\"shiki\"><div class=\"language-id\">json</div><div class='code-container'><code><span style=\"color: #000000\">{</span>\n<span style=\"color: #000000\">    </span><span style=\"color: #0451A5\">\"compilerOptions\"</span><span style=\"color: #000000\">: {</span>\n<span style=\"color: #000000\">        </span><span style=\"color: #0451A5\">\"target\"</span><span style=\"color: #000000\">: </span><span style=\"color: #A31515\">\"ES5\"</span><span style=\"color: #000000\">,</span>\n<span style=\"color: #000000\">        </span><span style=\"color: #0451A5\">\"experimentalDecorators\"</span><span style=\"color: #000000\">: </span><span style=\"color: #0000FF\">true</span>\n<span style=\"color: #000000\">    }</span>\n<span style=\"color: #000000\">}</span></code></div></pre>\n<h1 id=\"decorators\" style=\"position:relative;\"><a href=\"#decorators\" aria-label=\"decorators permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Decorators</h1>\n<p>A <em>Decorator</em> is a special kind of declaration that can be attached to a <a href=\"#class-decorators\">class declaration</a>, <a href=\"#method-decorators\">method</a>, <a href=\"#accessor-decorators\">accessor</a>, <a href=\"#property-decorators\">property</a>, or <a href=\"#parameter-decorators\">parameter</a>.\nDecorators use the form <code>@expression</code>, where <code>expression</code> must evaluate to a function that will be called at runtime with information about the decorated declaration.</p>\n<p>For example, given the decorator <code>@sealed</code> we might write the <code>sealed</code> function as follows:</p>\n<pre class=\"shiki\"><div class=\"language-id\">ts</div><div class='code-container'><code><span style=\"color: #0000FF\">function</span><span style=\"color: #000000\"> sealed(target) {</span>\n<span style=\"color: #000000\">    </span><span style=\"color: #008000\">// do something with 'target' ...</span>\n<span style=\"color: #000000\">}</span></code></div></pre>\n<blockquote>\n<p>NOTE  You can see a more detailed example of a decorator in <a href=\"#class-decorators\">Class Decorators</a>, below.</p>\n</blockquote>\n<h2 id=\"decorator-factories\" style=\"position:relative;\"><a href=\"#decorator-factories\" aria-label=\"decorator factories permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Decorator Factories</h2>\n<p>If we want to customize how a decorator is applied to a declaration, we can write a decorator factory.\nA <em>Decorator Factory</em> is simply a function that returns the expression that will be called by the decorator at runtime.</p>\n<p>We can write a decorator factory in the following fashion:</p>\n<pre class=\"shiki\"><div class=\"language-id\">ts</div><div class='code-container'><code><span style=\"color: #0000FF\">function</span><span style=\"color: #000000\"> color(value: string) { </span><span style=\"color: #008000\">// this is the decorator factory</span>\n<span style=\"color: #000000\">    </span><span style=\"color: #0000FF\">return</span><span style=\"color: #000000\"> </span><span style=\"color: #0000FF\">function</span><span style=\"color: #000000\"> (target) { </span><span style=\"color: #008000\">// this is the decorator</span>\n<span style=\"color: #000000\">        </span><span style=\"color: #008000\">// do something with 'target' and 'value'...</span>\n<span style=\"color: #000000\">    }</span>\n<span style=\"color: #000000\">}</span></code></div></pre>\n<blockquote>\n<p>NOTE  You can see a more detailed example of a decorator factory in <a href=\"#method-decorators\">Method Decorators</a>, below.</p>\n</blockquote>\n<h2 id=\"decorator-composition\" style=\"position:relative;\"><a href=\"#decorator-composition\" aria-label=\"decorator composition permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Decorator Composition</h2>\n<p>Multiple decorators can be applied to a declaration, as in the following examples:</p>\n<ul>\n<li>\n<p>On a single line:</p>\n<pre class=\"shiki\"><div class=\"language-id\">ts</div><div class='code-container'><code><span style=\"color: #000000\">@f @g x</span></code></div></pre>\n</li>\n<li>\n<p>On multiple lines:</p>\n<pre class=\"shiki\"><div class=\"language-id\">ts</div><div class='code-container'><code><span style=\"color: #000000\">@f</span>\n<span style=\"color: #000000\">@g</span>\n<span style=\"color: #000000\">x</span></code></div></pre>\n</li>\n</ul>\n<p>When multiple decorators apply to a single declaration, their evaluation is similar to <a href=\"http://en.wikipedia.org/wiki/Function_composition\">function composition in mathematics</a>. In this model, when composing functions <em>f</em> and <em>g</em>, the resulting composite (<em>f</em> ∘ <em>g</em>)(<em>x</em>) is equivalent to <em>f</em>(<em>g</em>(<em>x</em>)).</p>\n<p>As such, the following steps are performed when evaluating multiple decorators on a single declaration in TypeScript:</p>\n<ol>\n<li>The expressions for each decorator are evaluated top-to-bottom.</li>\n<li>The results are then called as functions from bottom-to-top.</li>\n</ol>\n<p>If we were to use <a href=\"#decorator-factories\">decorator factories</a>, we can observe this evaluation order with the following example:</p>\n<pre class=\"shiki\"><div class=\"language-id\">ts</div><div class='code-container'><code><span style=\"color: #0000FF\">function</span><span style=\"color: #000000\"> f() {</span>\n<span style=\"color: #000000\">    console.log(</span><span style=\"color: #A31515\">\"f(): evaluated\"</span><span style=\"color: #000000\">);</span>\n<span style=\"color: #000000\">    </span><span style=\"color: #0000FF\">return</span><span style=\"color: #000000\"> </span><span style=\"color: #0000FF\">function</span><span style=\"color: #000000\"> (target, propertyKey: string, descriptor: PropertyDescriptor) {</span>\n<span style=\"color: #000000\">        console.log(</span><span style=\"color: #A31515\">\"f(): called\"</span><span style=\"color: #000000\">);</span>\n<span style=\"color: #000000\">    }</span>\n<span style=\"color: #000000\">}</span>\n\n<span style=\"color: #0000FF\">function</span><span style=\"color: #000000\"> g() {</span>\n<span style=\"color: #000000\">    console.log(</span><span style=\"color: #A31515\">\"g(): evaluated\"</span><span style=\"color: #000000\">);</span>\n<span style=\"color: #000000\">    </span><span style=\"color: #0000FF\">return</span><span style=\"color: #000000\"> </span><span style=\"color: #0000FF\">function</span><span style=\"color: #000000\"> (target, propertyKey: string, descriptor: PropertyDescriptor) {</span>\n<span style=\"color: #000000\">        console.log(</span><span style=\"color: #A31515\">\"g(): called\"</span><span style=\"color: #000000\">);</span>\n<span style=\"color: #000000\">    }</span>\n<span style=\"color: #000000\">}</span>\n\n<span style=\"color: #0000FF\">class</span><span style=\"color: #000000\"> C {</span>\n<span style=\"color: #000000\">    @f()</span>\n<span style=\"color: #000000\">    @g()</span>\n<span style=\"color: #000000\">    method() {}</span>\n<span style=\"color: #000000\">}</span></code></div></pre>\n<p>Which would print this output to the console:</p>\n<pre class=\"shiki\"><div class=\"language-id\">shell</div><div class='code-container'><code><span style=\"color: #000000\">f(): evaluated</span>\n<span style=\"color: #000000\">g(): evaluated</span>\n<span style=\"color: #000000\">g(): called</span>\n<span style=\"color: #000000\">f(): called</span></code></div></pre>\n<h2 id=\"decorator-evaluation\" style=\"position:relative;\"><a href=\"#decorator-evaluation\" aria-label=\"decorator evaluation permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Decorator Evaluation</h2>\n<p>There is a well defined order to how decorators applied to various declarations inside of a class are applied:</p>\n<ol>\n<li><em>Parameter Decorators</em>, followed by <em>Method</em>, <em>Accessor</em>, or <em>Property Decorators</em> are applied for each instance member.</li>\n<li><em>Parameter Decorators</em>, followed by <em>Method</em>, <em>Accessor</em>, or <em>Property Decorators</em> are applied for each static member.</li>\n<li><em>Parameter Decorators</em> are applied for the constructor.</li>\n<li><em>Class Decorators</em> are applied for the class.</li>\n</ol>\n<h2 id=\"class-decorators\" style=\"position:relative;\"><a href=\"#class-decorators\" aria-label=\"class decorators permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Class Decorators</h2>\n<p>A <em>Class Decorator</em> is declared just before a class declaration.\nThe class decorator is applied to the constructor of the class and can be used to observe, modify, or replace a class definition.\nA class decorator cannot be used in a declaration file, or in any other ambient context (such as on a <code>declare</code> class).</p>\n<p>The expression for the class decorator will be called as a function at runtime, with the constructor of the decorated class as its only argument.</p>\n<p>If the class decorator returns a value, it will replace the class declaration with the provided constructor function.</p>\n<blockquote>\n<p>NOTE  Should you choose to return a new constructor function, you must take care to maintain the original prototype.\nThe logic that applies decorators at runtime will <strong>not</strong> do this for you.</p>\n</blockquote>\n<p>The following is an example of a class decorator (<code>@sealed</code>) applied to the <code>Greeter</code> class:</p>\n<pre class=\"shiki\"><div class=\"language-id\">ts</div><div class='code-container'><code><span style=\"color: #000000\">@sealed</span>\n<span style=\"color: #0000FF\">class</span><span style=\"color: #000000\"> Greeter {</span>\n<span style=\"color: #000000\">    greeting: string;</span>\n<span style=\"color: #000000\">    </span><span style=\"color: #0000FF\">constructor</span><span style=\"color: #000000\">(message: string) {</span>\n<span style=\"color: #000000\">        </span><span style=\"color: #0000FF\">this</span><span style=\"color: #000000\">.greeting = message;</span>\n<span style=\"color: #000000\">    }</span>\n<span style=\"color: #000000\">    greet() {</span>\n<span style=\"color: #000000\">        </span><span style=\"color: #0000FF\">return</span><span style=\"color: #000000\"> </span><span style=\"color: #A31515\">\"Hello, \"</span><span style=\"color: #000000\"> + </span><span style=\"color: #0000FF\">this</span><span style=\"color: #000000\">.greeting;</span>\n<span style=\"color: #000000\">    }</span>\n<span style=\"color: #000000\">}</span></code></div></pre>\n<p>We can define the <code>@sealed</code> decorator using the following function declaration:</p>\n<pre class=\"shiki\"><div class=\"language-id\">ts</div><div class='code-container'><code><span style=\"color: #0000FF\">function</span><span style=\"color: #000000\"> sealed(constructor: Function) {</span>\n<span style=\"color: #000000\">    Object.seal(constructor);</span>\n<span style=\"color: #000000\">    Object.seal(constructor.prototype);</span>\n<span style=\"color: #000000\">}</span></code></div></pre>\n<p>When <code>@sealed</code> is executed, it will seal both the constructor and its prototype.</p>\n<p>Next we have an example of how to override the constructor.</p>\n<pre class=\"shiki\"><div class=\"language-id\">ts</div><div class='code-container'><code><span style=\"color: #0000FF\">function</span><span style=\"color: #000000\"> classDecorator&lt;T </span><span style=\"color: #0000FF\">extends</span><span style=\"color: #000000\"> {</span><span style=\"color: #0000FF\">new</span><span style=\"color: #000000\">(...args:any[]):{}}&gt;(constructor:T) {</span>\n<span style=\"color: #000000\">    </span><span style=\"color: #0000FF\">return</span><span style=\"color: #000000\"> </span><span style=\"color: #0000FF\">class</span><span style=\"color: #000000\"> </span><span style=\"color: #0000FF\">extends</span><span style=\"color: #000000\"> constructor {</span>\n<span style=\"color: #000000\">        newProperty = </span><span style=\"color: #A31515\">\"new property\"</span><span style=\"color: #000000\">;</span>\n<span style=\"color: #000000\">        hello = </span><span style=\"color: #A31515\">\"override\"</span><span style=\"color: #000000\">;</span>\n<span style=\"color: #000000\">    }</span>\n<span style=\"color: #000000\">}</span>\n\n<span style=\"color: #000000\">@classDecorator</span>\n<span style=\"color: #0000FF\">class</span><span style=\"color: #000000\"> Greeter {</span>\n<span style=\"color: #000000\">    property = </span><span style=\"color: #A31515\">\"property\"</span><span style=\"color: #000000\">;</span>\n<span style=\"color: #000000\">    hello: string;</span>\n<span style=\"color: #000000\">    </span><span style=\"color: #0000FF\">constructor</span><span style=\"color: #000000\">(m: string) {</span>\n<span style=\"color: #000000\">        </span><span style=\"color: #0000FF\">this</span><span style=\"color: #000000\">.hello = m;</span>\n<span style=\"color: #000000\">    }</span>\n<span style=\"color: #000000\">}</span>\n\n<span style=\"color: #000000\">console.log(</span><span style=\"color: #0000FF\">new</span><span style=\"color: #000000\"> Greeter(</span><span style=\"color: #A31515\">\"world\"</span><span style=\"color: #000000\">));</span></code></div></pre>\n<h2 id=\"method-decorators\" style=\"position:relative;\"><a href=\"#method-decorators\" aria-label=\"method decorators permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Method Decorators</h2>\n<p>A <em>Method Decorator</em> is declared just before a method declaration.\nThe decorator is applied to the <em>Property Descriptor</em> for the method, and can be used to observe, modify, or replace a method definition.\nA method decorator cannot be used in a declaration file, on an overload, or in any other ambient context (such as in a <code>declare</code> class).</p>\n<p>The expression for the method decorator will be called as a function at runtime, with the following three arguments:</p>\n<ol>\n<li>Either the constructor function of the class for a static member, or the prototype of the class for an instance member.</li>\n<li>The name of the member.</li>\n<li>The <em>Property Descriptor</em> for the member.</li>\n</ol>\n<blockquote>\n<p>NOTE  The <em>Property Descriptor</em> will be <code>undefined</code> if your script target is less than <code>ES5</code>.</p>\n</blockquote>\n<p>If the method decorator returns a value, it will be used as the <em>Property Descriptor</em> for the method.</p>\n<blockquote>\n<p>NOTE  The return value is ignored if your script target is less than <code>ES5</code>.</p>\n</blockquote>\n<p>The following is an example of a method decorator (<code>@enumerable</code>) applied to a method on the <code>Greeter</code> class:</p>\n<pre class=\"shiki\"><div class=\"language-id\">ts</div><div class='code-container'><code><span style=\"color: #0000FF\">class</span><span style=\"color: #000000\"> Greeter {</span>\n<span style=\"color: #000000\">    greeting: string;</span>\n<span style=\"color: #000000\">    </span><span style=\"color: #0000FF\">constructor</span><span style=\"color: #000000\">(message: string) {</span>\n<span style=\"color: #000000\">        </span><span style=\"color: #0000FF\">this</span><span style=\"color: #000000\">.greeting = message;</span>\n<span style=\"color: #000000\">    }</span>\n\n<span style=\"color: #000000\">    @enumerable(</span><span style=\"color: #0000FF\">false</span><span style=\"color: #000000\">)</span>\n<span style=\"color: #000000\">    greet() {</span>\n<span style=\"color: #000000\">        </span><span style=\"color: #0000FF\">return</span><span style=\"color: #000000\"> </span><span style=\"color: #A31515\">\"Hello, \"</span><span style=\"color: #000000\"> + </span><span style=\"color: #0000FF\">this</span><span style=\"color: #000000\">.greeting;</span>\n<span style=\"color: #000000\">    }</span>\n<span style=\"color: #000000\">}</span></code></div></pre>\n<p>We can define the <code>@enumerable</code> decorator using the following function declaration:</p>\n<pre class=\"shiki\"><div class=\"language-id\">ts</div><div class='code-container'><code><span style=\"color: #0000FF\">function</span><span style=\"color: #000000\"> enumerable(value: boolean) {</span>\n<span style=\"color: #000000\">    </span><span style=\"color: #0000FF\">return</span><span style=\"color: #000000\"> </span><span style=\"color: #0000FF\">function</span><span style=\"color: #000000\"> (target: any, propertyKey: string, descriptor: PropertyDescriptor) {</span>\n<span style=\"color: #000000\">        descriptor.enumerable = value;</span>\n<span style=\"color: #000000\">    };</span>\n<span style=\"color: #000000\">}</span></code></div></pre>\n<p>The <code>@enumerable(false)</code> decorator here is a <a href=\"#decorator-factories\">decorator factory</a>.\nWhen the <code>@enumerable(false)</code> decorator is called, it modifies the <code>enumerable</code> property of the property descriptor.</p>\n<h2 id=\"accessor-decorators\" style=\"position:relative;\"><a href=\"#accessor-decorators\" aria-label=\"accessor decorators permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Accessor Decorators</h2>\n<p>An <em>Accessor Decorator</em> is declared just before an accessor declaration.\nThe accessor decorator is applied to the <em>Property Descriptor</em> for the accessor and can be used to observe, modify, or replace an accessor’s definitions.\nAn accessor decorator cannot be used in a declaration file, or in any other ambient context (such as in a <code>declare</code> class).</p>\n<blockquote>\n<p>NOTE  TypeScript disallows decorating both the <code>get</code> and <code>set</code> accessor for a single member.\nInstead, all decorators for the member must be applied to the first accessor specified in document order.\nThis is because decorators apply to a <em>Property Descriptor</em>, which combines both the <code>get</code> and <code>set</code> accessor, not each declaration separately.</p>\n</blockquote>\n<p>The expression for the accessor decorator will be called as a function at runtime, with the following three arguments:</p>\n<ol>\n<li>Either the constructor function of the class for a static member, or the prototype of the class for an instance member.</li>\n<li>The name of the member.</li>\n<li>The <em>Property Descriptor</em> for the member.</li>\n</ol>\n<blockquote>\n<p>NOTE  The <em>Property Descriptor</em> will be <code>undefined</code> if your script target is less than <code>ES5</code>.</p>\n</blockquote>\n<p>If the accessor decorator returns a value, it will be used as the <em>Property Descriptor</em> for the member.</p>\n<blockquote>\n<p>NOTE  The return value is ignored if your script target is less than <code>ES5</code>.</p>\n</blockquote>\n<p>The following is an example of an accessor decorator (<code>@configurable</code>) applied to a member of the <code>Point</code> class:</p>\n<pre class=\"shiki\"><div class=\"language-id\">ts</div><div class='code-container'><code><span style=\"color: #0000FF\">class</span><span style=\"color: #000000\"> Point {</span>\n<span style=\"color: #000000\">    </span><span style=\"color: #0000FF\">private</span><span style=\"color: #000000\"> _x: number;</span>\n<span style=\"color: #000000\">    </span><span style=\"color: #0000FF\">private</span><span style=\"color: #000000\"> _y: number;</span>\n<span style=\"color: #000000\">    </span><span style=\"color: #0000FF\">constructor</span><span style=\"color: #000000\">(x: number, y: number) {</span>\n<span style=\"color: #000000\">        </span><span style=\"color: #0000FF\">this</span><span style=\"color: #000000\">._x = x;</span>\n<span style=\"color: #000000\">        </span><span style=\"color: #0000FF\">this</span><span style=\"color: #000000\">._y = y;</span>\n<span style=\"color: #000000\">    }</span>\n\n<span style=\"color: #000000\">    @configurable(</span><span style=\"color: #0000FF\">false</span><span style=\"color: #000000\">)</span>\n<span style=\"color: #000000\">    </span><span style=\"color: #0000FF\">get</span><span style=\"color: #000000\"> x() { </span><span style=\"color: #0000FF\">return</span><span style=\"color: #000000\"> </span><span style=\"color: #0000FF\">this</span><span style=\"color: #000000\">._x; }</span>\n\n<span style=\"color: #000000\">    @configurable(</span><span style=\"color: #0000FF\">false</span><span style=\"color: #000000\">)</span>\n<span style=\"color: #000000\">    </span><span style=\"color: #0000FF\">get</span><span style=\"color: #000000\"> y() { </span><span style=\"color: #0000FF\">return</span><span style=\"color: #000000\"> </span><span style=\"color: #0000FF\">this</span><span style=\"color: #000000\">._y; }</span>\n<span style=\"color: #000000\">}</span></code></div></pre>\n<p>We can define the <code>@configurable</code> decorator using the following function declaration:</p>\n<pre class=\"shiki\"><div class=\"language-id\">ts</div><div class='code-container'><code><span style=\"color: #0000FF\">function</span><span style=\"color: #000000\"> configurable(value: boolean) {</span>\n<span style=\"color: #000000\">    </span><span style=\"color: #0000FF\">return</span><span style=\"color: #000000\"> </span><span style=\"color: #0000FF\">function</span><span style=\"color: #000000\"> (target: any, propertyKey: string, descriptor: PropertyDescriptor) {</span>\n<span style=\"color: #000000\">        descriptor.configurable = value;</span>\n<span style=\"color: #000000\">    };</span>\n<span style=\"color: #000000\">}</span></code></div></pre>\n<h2 id=\"property-decorators\" style=\"position:relative;\"><a href=\"#property-decorators\" aria-label=\"property decorators permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Property Decorators</h2>\n<p>A <em>Property Decorator</em> is declared just before a property declaration.\nA property decorator cannot be used in a declaration file, or in any other ambient context (such as in a <code>declare</code> class).</p>\n<p>The expression for the property decorator will be called as a function at runtime, with the following two arguments:</p>\n<ol>\n<li>Either the constructor function of the class for a static member, or the prototype of the class for an instance member.</li>\n<li>The name of the member.</li>\n</ol>\n<blockquote>\n<p>NOTE  A <em>Property Descriptor</em> is not provided as an argument to a property decorator due to how property decorators are initialized in TypeScript.\nThis is because there is currently no mechanism to describe an instance property when defining members of a prototype, and no way to observe or modify the initializer for a property. The return value is ignored too.\nAs such, a property decorator can only be used to observe that a property of a specific name has been declared for a class.</p>\n</blockquote>\n<p>We can use this information to record metadata about the property, as in the following example:</p>\n<pre class=\"shiki\"><div class=\"language-id\">ts</div><div class='code-container'><code><span style=\"color: #0000FF\">class</span><span style=\"color: #000000\"> Greeter {</span>\n<span style=\"color: #000000\">    @format(</span><span style=\"color: #A31515\">\"Hello, %s\"</span><span style=\"color: #000000\">)</span>\n<span style=\"color: #000000\">    greeting: string;</span>\n\n<span style=\"color: #000000\">    </span><span style=\"color: #0000FF\">constructor</span><span style=\"color: #000000\">(message: string) {</span>\n<span style=\"color: #000000\">        </span><span style=\"color: #0000FF\">this</span><span style=\"color: #000000\">.greeting = message;</span>\n<span style=\"color: #000000\">    }</span>\n<span style=\"color: #000000\">    greet() {</span>\n<span style=\"color: #000000\">        </span><span style=\"color: #0000FF\">let</span><span style=\"color: #000000\"> formatString = getFormat(</span><span style=\"color: #0000FF\">this</span><span style=\"color: #000000\">, </span><span style=\"color: #A31515\">\"greeting\"</span><span style=\"color: #000000\">);</span>\n<span style=\"color: #000000\">        </span><span style=\"color: #0000FF\">return</span><span style=\"color: #000000\"> formatString.replace(</span><span style=\"color: #A31515\">\"%s\"</span><span style=\"color: #000000\">, </span><span style=\"color: #0000FF\">this</span><span style=\"color: #000000\">.greeting);</span>\n<span style=\"color: #000000\">    }</span>\n<span style=\"color: #000000\">}</span></code></div></pre>\n<p>We can then define the <code>@format</code> decorator and <code>getFormat</code> functions using the following function declarations:</p>\n<pre class=\"shiki\"><div class=\"language-id\">ts</div><div class='code-container'><code><span style=\"color: #0000FF\">import</span><span style=\"color: #000000\"> </span><span style=\"color: #A31515\">\"reflect-metadata\"</span><span style=\"color: #000000\">;</span>\n\n<span style=\"color: #0000FF\">const</span><span style=\"color: #000000\"> formatMetadataKey = Symbol(</span><span style=\"color: #A31515\">\"format\"</span><span style=\"color: #000000\">);</span>\n\n<span style=\"color: #0000FF\">function</span><span style=\"color: #000000\"> format(formatString: string) {</span>\n<span style=\"color: #000000\">    </span><span style=\"color: #0000FF\">return</span><span style=\"color: #000000\"> Reflect.metadata(formatMetadataKey, formatString);</span>\n<span style=\"color: #000000\">}</span>\n\n<span style=\"color: #0000FF\">function</span><span style=\"color: #000000\"> getFormat(target: any, propertyKey: string) {</span>\n<span style=\"color: #000000\">    </span><span style=\"color: #0000FF\">return</span><span style=\"color: #000000\"> Reflect.getMetadata(formatMetadataKey, target, propertyKey);</span>\n<span style=\"color: #000000\">}</span></code></div></pre>\n<p>The <code>@format(\"Hello, %s\")</code> decorator here is a <a href=\"#decorator-factories\">decorator factory</a>.\nWhen <code>@format(\"Hello, %s\")</code> is called, it adds a metadata entry for the property using the <code>Reflect.metadata</code> function from the <code>reflect-metadata</code> library.\nWhen <code>getFormat</code> is called, it reads the metadata value for the format.</p>\n<blockquote>\n<p>NOTE  This example requires the <code>reflect-metadata</code> library.\nSee <a href=\"#metadata\">Metadata</a> for more information about the <code>reflect-metadata</code> library.</p>\n</blockquote>\n<h2 id=\"parameter-decorators\" style=\"position:relative;\"><a href=\"#parameter-decorators\" aria-label=\"parameter decorators permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Parameter Decorators</h2>\n<p>A <em>Parameter Decorator</em> is declared just before a parameter declaration.\nThe parameter decorator is applied to the function for a class constructor or method declaration.\nA parameter decorator cannot be used in a declaration file, an overload, or in any other ambient context (such as in a <code>declare</code> class).</p>\n<p>The expression for the parameter decorator will be called as a function at runtime, with the following three arguments:</p>\n<ol>\n<li>Either the constructor function of the class for a static member, or the prototype of the class for an instance member.</li>\n<li>The name of the member.</li>\n<li>The ordinal index of the parameter in the function’s parameter list.</li>\n</ol>\n<blockquote>\n<p>NOTE  A parameter decorator can only be used to observe that a parameter has been declared on a method.</p>\n</blockquote>\n<p>The return value of the parameter decorator is ignored.</p>\n<p>The following is an example of a parameter decorator (<code>@required</code>) applied to parameter of a member of the <code>Greeter</code> class:</p>\n<pre class=\"shiki\"><div class=\"language-id\">ts</div><div class='code-container'><code><span style=\"color: #0000FF\">class</span><span style=\"color: #000000\"> Greeter {</span>\n<span style=\"color: #000000\">    greeting: string;</span>\n\n<span style=\"color: #000000\">    </span><span style=\"color: #0000FF\">constructor</span><span style=\"color: #000000\">(message: string) {</span>\n<span style=\"color: #000000\">        </span><span style=\"color: #0000FF\">this</span><span style=\"color: #000000\">.greeting = message;</span>\n<span style=\"color: #000000\">    }</span>\n\n<span style=\"color: #000000\">    @validate</span>\n<span style=\"color: #000000\">    greet(@required name: string) {</span>\n<span style=\"color: #000000\">        </span><span style=\"color: #0000FF\">return</span><span style=\"color: #000000\"> </span><span style=\"color: #A31515\">\"Hello \"</span><span style=\"color: #000000\"> + name + </span><span style=\"color: #A31515\">\", \"</span><span style=\"color: #000000\"> + </span><span style=\"color: #0000FF\">this</span><span style=\"color: #000000\">.greeting;</span>\n<span style=\"color: #000000\">    }</span>\n<span style=\"color: #000000\">}</span></code></div></pre>\n<p>We can then define the <code>@required</code> and <code>@validate</code> decorators using the following function declarations:</p>\n<pre class=\"shiki\"><div class=\"language-id\">ts</div><div class='code-container'><code><span style=\"color: #0000FF\">import</span><span style=\"color: #000000\"> </span><span style=\"color: #A31515\">\"reflect-metadata\"</span><span style=\"color: #000000\">;</span>\n\n<span style=\"color: #0000FF\">const</span><span style=\"color: #000000\"> requiredMetadataKey = Symbol(</span><span style=\"color: #A31515\">\"required\"</span><span style=\"color: #000000\">);</span>\n\n<span style=\"color: #0000FF\">function</span><span style=\"color: #000000\"> required(target: Object, propertyKey: string | symbol, parameterIndex: number) {</span>\n<span style=\"color: #000000\">    </span><span style=\"color: #0000FF\">let</span><span style=\"color: #000000\"> existingRequiredParameters: number[] = Reflect.getOwnMetadata(requiredMetadataKey, target, propertyKey) || [];</span>\n<span style=\"color: #000000\">    existingRequiredParameters.push(parameterIndex);</span>\n<span style=\"color: #000000\">    Reflect.defineMetadata(requiredMetadataKey, existingRequiredParameters, target, propertyKey);</span>\n<span style=\"color: #000000\">}</span>\n\n<span style=\"color: #0000FF\">function</span><span style=\"color: #000000\"> validate(target: any, propertyName: string, descriptor: TypedPropertyDescriptor&lt;Function&gt;) {</span>\n<span style=\"color: #000000\">    </span><span style=\"color: #0000FF\">let</span><span style=\"color: #000000\"> method = descriptor.value;</span>\n<span style=\"color: #000000\">    descriptor.value = </span><span style=\"color: #0000FF\">function</span><span style=\"color: #000000\"> () {</span>\n<span style=\"color: #000000\">        </span><span style=\"color: #0000FF\">let</span><span style=\"color: #000000\"> requiredParameters: number[] = Reflect.getOwnMetadata(requiredMetadataKey, target, propertyName);</span>\n<span style=\"color: #000000\">        </span><span style=\"color: #0000FF\">if</span><span style=\"color: #000000\"> (requiredParameters) {</span>\n<span style=\"color: #000000\">            </span><span style=\"color: #0000FF\">for</span><span style=\"color: #000000\"> (</span><span style=\"color: #0000FF\">let</span><span style=\"color: #000000\"> parameterIndex </span><span style=\"color: #0000FF\">of</span><span style=\"color: #000000\"> requiredParameters) {</span>\n<span style=\"color: #000000\">                </span><span style=\"color: #0000FF\">if</span><span style=\"color: #000000\"> (parameterIndex &gt;= </span><span style=\"color: #0000FF\">arguments</span><span style=\"color: #000000\">.length || </span><span style=\"color: #0000FF\">arguments</span><span style=\"color: #000000\">[parameterIndex] === </span><span style=\"color: #0000FF\">undefined</span><span style=\"color: #000000\">) {</span>\n<span style=\"color: #000000\">                    </span><span style=\"color: #0000FF\">throw</span><span style=\"color: #000000\"> </span><span style=\"color: #0000FF\">new</span><span style=\"color: #000000\"> Error(</span><span style=\"color: #A31515\">\"Missing required argument.\"</span><span style=\"color: #000000\">);</span>\n<span style=\"color: #000000\">                }</span>\n<span style=\"color: #000000\">            }</span>\n<span style=\"color: #000000\">        }</span>\n\n<span style=\"color: #000000\">        </span><span style=\"color: #0000FF\">return</span><span style=\"color: #000000\"> method.apply(</span><span style=\"color: #0000FF\">this</span><span style=\"color: #000000\">, </span><span style=\"color: #0000FF\">arguments</span><span style=\"color: #000000\">);</span>\n<span style=\"color: #000000\">    }</span>\n<span style=\"color: #000000\">}</span></code></div></pre>\n<p>The <code>@required</code> decorator adds a metadata entry that marks the parameter as required.\nThe <code>@validate</code> decorator then wraps the existing <code>greet</code> method in a function that validates the arguments before invoking the original method.</p>\n<blockquote>\n<p>NOTE  This example requires the <code>reflect-metadata</code> library.\nSee <a href=\"#metadata\">Metadata</a> for more information about the <code>reflect-metadata</code> library.</p>\n</blockquote>\n<h2 id=\"metadata\" style=\"position:relative;\"><a href=\"#metadata\" aria-label=\"metadata permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Metadata</h2>\n<p>Some examples use the <code>reflect-metadata</code> library which adds a polyfill for an <a href=\"https://github.com/rbuckton/ReflectDecorators\">experimental metadata API</a>.\nThis library is not yet part of the ECMAScript (JavaScript) standard.\nHowever, once decorators are officially adopted as part of the ECMAScript standard these extensions will be proposed for adoption.</p>\n<p>You can install this library via npm:</p>\n<pre class=\"shiki\"><div class=\"language-id\">shell</div><div class='code-container'><code><span style=\"color: #000000\">npm i reflect-metadata --save</span></code></div></pre>\n<p>TypeScript includes experimental support for emitting certain types of metadata for declarations that have decorators.\nTo enable this experimental support, you must set the <code>emitDecoratorMetadata</code> compiler option either on the command line or in your <code>tsconfig.json</code>:</p>\n<p><strong>Command Line</strong>:</p>\n<pre class=\"shiki\"><div class=\"language-id\">shell</div><div class='code-container'><code><span style=\"color: #000000\">tsc --target ES5 --experimentalDecorators --emitDecoratorMetadata</span></code></div></pre>\n<p><strong>tsconfig.json</strong>:</p>\n<pre class=\"shiki\"><div class=\"language-id\">json</div><div class='code-container'><code><span style=\"color: #000000\">{</span>\n<span style=\"color: #000000\">    </span><span style=\"color: #0451A5\">\"compilerOptions\"</span><span style=\"color: #000000\">: {</span>\n<span style=\"color: #000000\">        </span><span style=\"color: #0451A5\">\"target\"</span><span style=\"color: #000000\">: </span><span style=\"color: #A31515\">\"ES5\"</span><span style=\"color: #000000\">,</span>\n<span style=\"color: #000000\">        </span><span style=\"color: #0451A5\">\"experimentalDecorators\"</span><span style=\"color: #000000\">: </span><span style=\"color: #0000FF\">true</span><span style=\"color: #000000\">,</span>\n<span style=\"color: #000000\">        </span><span style=\"color: #0451A5\">\"emitDecoratorMetadata\"</span><span style=\"color: #000000\">: </span><span style=\"color: #0000FF\">true</span>\n<span style=\"color: #000000\">    }</span>\n<span style=\"color: #000000\">}</span></code></div></pre>\n<p>When enabled, as long as the <code>reflect-metadata</code> library has been imported, additional design-time type information will be exposed at runtime.</p>\n<p>We can see this in action in the following example:</p>\n<pre class=\"shiki\"><div class=\"language-id\">ts</div><div class='code-container'><code><span style=\"color: #0000FF\">import</span><span style=\"color: #000000\"> </span><span style=\"color: #A31515\">\"reflect-metadata\"</span><span style=\"color: #000000\">;</span>\n\n<span style=\"color: #0000FF\">class</span><span style=\"color: #000000\"> Point {</span>\n<span style=\"color: #000000\">    x: number;</span>\n<span style=\"color: #000000\">    y: number;</span>\n<span style=\"color: #000000\">}</span>\n\n<span style=\"color: #0000FF\">class</span><span style=\"color: #000000\"> Line {</span>\n<span style=\"color: #000000\">    </span><span style=\"color: #0000FF\">private</span><span style=\"color: #000000\"> _p0: Point;</span>\n<span style=\"color: #000000\">    </span><span style=\"color: #0000FF\">private</span><span style=\"color: #000000\"> _p1: Point;</span>\n\n<span style=\"color: #000000\">    @validate</span>\n<span style=\"color: #000000\">    </span><span style=\"color: #0000FF\">set</span><span style=\"color: #000000\"> p0(value: Point) { </span><span style=\"color: #0000FF\">this</span><span style=\"color: #000000\">._p0 = value; }</span>\n<span style=\"color: #000000\">    </span><span style=\"color: #0000FF\">get</span><span style=\"color: #000000\"> p0() { </span><span style=\"color: #0000FF\">return</span><span style=\"color: #000000\"> </span><span style=\"color: #0000FF\">this</span><span style=\"color: #000000\">._p0; }</span>\n\n<span style=\"color: #000000\">    @validate</span>\n<span style=\"color: #000000\">    </span><span style=\"color: #0000FF\">set</span><span style=\"color: #000000\"> p1(value: Point) { </span><span style=\"color: #0000FF\">this</span><span style=\"color: #000000\">._p1 = value; }</span>\n<span style=\"color: #000000\">    </span><span style=\"color: #0000FF\">get</span><span style=\"color: #000000\"> p1() { </span><span style=\"color: #0000FF\">return</span><span style=\"color: #000000\"> </span><span style=\"color: #0000FF\">this</span><span style=\"color: #000000\">._p1; }</span>\n<span style=\"color: #000000\">}</span>\n\n<span style=\"color: #0000FF\">function</span><span style=\"color: #000000\"> validate&lt;T&gt;(target: any, propertyKey: string, descriptor: TypedPropertyDescriptor&lt;T&gt;) {</span>\n<span style=\"color: #000000\">    </span><span style=\"color: #0000FF\">let</span><span style=\"color: #000000\"> set = descriptor.set;</span>\n<span style=\"color: #000000\">    descriptor.set = </span><span style=\"color: #0000FF\">function</span><span style=\"color: #000000\"> (value: T) {</span>\n<span style=\"color: #000000\">        </span><span style=\"color: #0000FF\">let</span><span style=\"color: #000000\"> type = Reflect.getMetadata(</span><span style=\"color: #A31515\">\"design:type\"</span><span style=\"color: #000000\">, target, propertyKey);</span>\n<span style=\"color: #000000\">        </span><span style=\"color: #0000FF\">if</span><span style=\"color: #000000\"> (!(value </span><span style=\"color: #0000FF\">instanceof</span><span style=\"color: #000000\"> type)) {</span>\n<span style=\"color: #000000\">            </span><span style=\"color: #0000FF\">throw</span><span style=\"color: #000000\"> </span><span style=\"color: #0000FF\">new</span><span style=\"color: #000000\"> TypeError(</span><span style=\"color: #A31515\">\"Invalid type.\"</span><span style=\"color: #000000\">);</span>\n<span style=\"color: #000000\">        }</span>\n<span style=\"color: #000000\">        set.call(target, value);</span>\n<span style=\"color: #000000\">    }</span>\n<span style=\"color: #000000\">}</span></code></div></pre>\n<p>The TypeScript compiler will inject design-time type information using the <code>@Reflect.metadata</code> decorator.\nYou could consider it the equivalent of the following TypeScript:</p>\n<pre class=\"shiki\"><div class=\"language-id\">ts</div><div class='code-container'><code><span style=\"color: #0000FF\">class</span><span style=\"color: #000000\"> Line {</span>\n<span style=\"color: #000000\">    </span><span style=\"color: #0000FF\">private</span><span style=\"color: #000000\"> _p0: Point;</span>\n<span style=\"color: #000000\">    </span><span style=\"color: #0000FF\">private</span><span style=\"color: #000000\"> _p1: Point;</span>\n\n<span style=\"color: #000000\">    @validate</span>\n<span style=\"color: #000000\">    @Reflect.metadata(</span><span style=\"color: #A31515\">\"design:type\"</span><span style=\"color: #000000\">, Point)</span>\n<span style=\"color: #000000\">    </span><span style=\"color: #0000FF\">set</span><span style=\"color: #000000\"> p0(value: Point) { </span><span style=\"color: #0000FF\">this</span><span style=\"color: #000000\">._p0 = value; }</span>\n<span style=\"color: #000000\">    </span><span style=\"color: #0000FF\">get</span><span style=\"color: #000000\"> p0() { </span><span style=\"color: #0000FF\">return</span><span style=\"color: #000000\"> </span><span style=\"color: #0000FF\">this</span><span style=\"color: #000000\">._p0; }</span>\n\n<span style=\"color: #000000\">    @validate</span>\n<span style=\"color: #000000\">    @Reflect.metadata(</span><span style=\"color: #A31515\">\"design:type\"</span><span style=\"color: #000000\">, Point)</span>\n<span style=\"color: #000000\">    </span><span style=\"color: #0000FF\">set</span><span style=\"color: #000000\"> p1(value: Point) { </span><span style=\"color: #0000FF\">this</span><span style=\"color: #000000\">._p1 = value; }</span>\n<span style=\"color: #000000\">    </span><span style=\"color: #0000FF\">get</span><span style=\"color: #000000\"> p1() { </span><span style=\"color: #0000FF\">return</span><span style=\"color: #000000\"> </span><span style=\"color: #0000FF\">this</span><span style=\"color: #000000\">._p1; }</span>\n<span style=\"color: #000000\">}</span></code></div></pre>\n<blockquote>\n<p>NOTE  Decorator metadata is an experimental feature and may introduce breaking changes in future releases.</p>\n</blockquote>","headings":[{"value":"Introduction","depth":1},{"value":"Decorators","depth":1},{"value":"Decorator Factories","depth":2},{"value":"Decorator Composition","depth":2},{"value":"Decorator Evaluation","depth":2},{"value":"Class Decorators","depth":2},{"value":"Method Decorators","depth":2},{"value":"Accessor Decorators","depth":2},{"value":"Property Decorators","depth":2},{"value":"Parameter Decorators","depth":2},{"value":"Metadata","depth":2}],"frontmatter":{"permalink":"/docs/handbook/decorators.html","title":"Decorators"}}},"pageContext":{"slug":"/docs/handbook/decorators.html","isOldHandbook":true}}}