Parameter DecoratorsĪ Parameter Decorator is declared just before a parameter declaration. See Metadata for more information about the reflect-metadata library. NOTE This example requires the reflect-metadata library. When getFormat is called, it reads the metadata value for the format. When %s") is called, it adds a metadata entry for the property using the tadata function from the reflect-metadata library. The %s") decorator here is a decorator factory. We can use this information to record metadata about the property, as in the following example: The return value is ignored too.Īs such, a property decorator can only be used to observe that a property of a specific name has been declared for a class. This 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. NOTE A Property Descriptor is not provided as an argument to a property decorator due to how property decorators are initialized in TypeScript. The expression for the property decorator will be called as a function at runtime, with the following two arguments: The following is an example of an accessor decorator ( applied to a member of the Point class:Ī Property Decorator is declared just before a property declaration.Ī property decorator cannot be used in a declaration file, or in any other ambient context (such as in a declare class). If the accessor decorator returns a value, it will be used as the Property Descriptor for the member. The expression for the accessor decorator will be called as a function at runtime, with the following three arguments: This is because decorators apply to a Property Descriptor, which combines both the get and set accessor, not each declaration separately. Instead, all decorators for the member must be applied to the first accessor specified in document order. NOTE TypeScript disallows decorating both the get and set accessor for a single member. The accessor decorator is applied to the Property Descriptor for the accessor and can be used to observe, modify, or replace an accessor’s definitions.Īn accessor decorator cannot be used in a declaration file, or in any other ambient context (such as in a declare class). Accessor DecoratorsĪn Accessor Decorator is declared just before an accessor declaration. When the decorator is called, it modifies the enumerable property of the property descriptor. The decorator here is a decorator factory. The following is an example of a method decorator ( applied to a method on the Greeter class: NOTE The return value is ignored if your script target is less than ES5. If the method decorator returns a value, it will be used as the Property Descriptor for the method. NOTE The Property Descriptor will be undefined if your script target is less than ES5. The Property Descriptor for the member. Either the constructor function of the class for a static member, or the prototype of the class for an instance member.The expression for the method decorator will be called as a function at runtime, with the following three arguments: The decorator is applied to the Property Descriptor for the method, and can be used to observe, modify, or replace a method definition.Ī method decorator cannot be used in a declaration file, on an overload, or in any other ambient context (such as in a declare class). Try Method DecoratorsĪ Method Decorator is declared just before a method declaration. 2339 Property 'reportingURL' does not exist on type 'BugReport'. reportingURL Property 'reportingURL' does not exist on type 'BugReport'. The following is an example of a class decorator ( applied to a BugReport class:īug. The logic that applies decorators at runtime will not do this for you. NOTE Should you choose to return a new constructor function, you must take care to maintain the original prototype. If the class decorator returns a value, it will replace the class declaration with the provided constructor function. 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. The class decorator is applied to the constructor of the class and can be used to observe, modify, or replace a class definition.Ī class decorator cannot be used in a declaration file, or in any other ambient context (such as on a declare class).
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |