Please enable JavaScript.
Coggle requires JavaScript to display documents.
TypeScript - Advanced Concepts (Decorators (Usage (Multiple decorators…
TypeScript - Advanced Concepts
Decorators
Provide annotations and a meta-programming interface for class declarations and class members
Class decorators
Constructor of the decorated class is passed as a parameter
Class declaration would be replaced if a constructor is returned from the decorator
Usage
Decorator expression must evaluate to a function that's called at runtime
A decorator factory is a function that returns a decorator
Multiple decorators
Can be applied in a single line or multiple lines
Evaluated top to bottom and returned bottom to top
Cannot be used in ambient contexts
Can be used to observe, modify, or replace a definition
Method and accessor decorators
Parameters
Member name
Property descriptor
Constructor (for static methods) or the prototype (for instance methods)
Accessor decorators
All decorators for a single member should be applied to the first accessor in document order
The property descriptor would be replaced if a value is returned from the constructor
Property and parameter decorators
Parameters
Constructor (for static methods) or the prototype (for instance methods)
Member name
Parameter decorators
Index of the parameter
Can only be used to observe that a property or parameter of its name has been declared
Return value is ignored
Metadata
reflect-metadata library has to be imported
Compiler options
--experimentalDecorators
--emitDecoratorMetadata
Further reference
Sealing objects
Property descriptors
reflect-metadata library
Advanced types
Intersection types
type & anotherType
Values have all members of the given types
Used for mixins
Union types
type | anotherType
A type-safe alternative to any
Describes a value that can be one of several types
Testing for type
Type assertions can be used to test the existence of properties
Only members that are common to all given types are accessible
Type guards
An expressions that performs a runtime check that guarantees a type in some scope
Type predicates
parameterName is Type
Returned by a tester function
Narrow a value to a specific type if it's compatible
in operator
'property' in value
typeof operator
typeof value === 'typeName'
Only supports number, string, boolean, and symbol
instanceof operator
value instanceof type
Nullable types