UNPKG

3.94 kBTypeScriptView Raw
1/**
2 * @license
3 * Copyright Google Inc. All Rights Reserved.
4 *
5 * Use of this source code is governed by an MIT-style license that can be
6 * found in the LICENSE file at https://angular.io/license
7 */
8import { Type } from '../facade/type';
9/**
10 * Declares the interface to be used with {@link Class}.
11 *
12 * @stable
13 */
14export declare type ClassDefinition = {
15 /**
16 * Optional argument for specifying the superclass.
17 */
18 extends?: Type<any>;
19 /**
20 * Required constructor function for a class.
21 *
22 * The function may be optionally wrapped in an `Array`, in which case additional parameter
23 * annotations may be specified.
24 * The number of arguments and the number of parameter annotations must match.
25 *
26 * See {@link Class} for example of usage.
27 */
28 constructor: Function | any[];
29} & {
30 /**
31 * Other methods on the class. Note that values should have type 'Function' but TS requires
32 * all properties to have a narrower type than the index signature.
33 */
34 [x: string]: Type<any> | Function | any[];
35};
36/**
37 * An interface implemented by all Angular type decorators, which allows them to be used as ES7
38 * decorators as well as
39 * Angular DSL syntax.
40 *
41 * DSL syntax:
42 *
43 * ```
44 * var MyClass = ng
45 * .Component({...})
46 * .Class({...});
47 * ```
48 *
49 * ES7 syntax:
50 *
51 * ```
52 * @ng.Component({...})
53 * class MyClass {...}
54 * ```
55 * @stable
56 */
57export interface TypeDecorator {
58 /**
59 * Invoke as ES7 decorator.
60 */
61 <T extends Type<any>>(type: T): T;
62 (target: Object, propertyKey?: string | symbol, parameterIndex?: number): void;
63 /**
64 * Storage for the accumulated annotations so far used by the DSL syntax.
65 *
66 * Used by {@link Class} to annotate the generated class.
67 */
68 annotations: any[];
69 /**
70 * Generate a class from the definition and annotate it with {@link TypeDecorator#annotations}.
71 */
72 Class(obj: ClassDefinition): Type<any>;
73}
74/**
75 * Provides a way for expressing ES6 classes with parameter annotations in ES5.
76 *
77 * ## Basic Example
78 *
79 * ```
80 * var Greeter = ng.Class({
81 * constructor: function(name) {
82 * this.name = name;
83 * },
84 *
85 * greet: function() {
86 * alert('Hello ' + this.name + '!');
87 * }
88 * });
89 * ```
90 *
91 * is equivalent to ES6:
92 *
93 * ```
94 * class Greeter {
95 * constructor(name) {
96 * this.name = name;
97 * }
98 *
99 * greet() {
100 * alert('Hello ' + this.name + '!');
101 * }
102 * }
103 * ```
104 *
105 * or equivalent to ES5:
106 *
107 * ```
108 * var Greeter = function (name) {
109 * this.name = name;
110 * }
111 *
112 * Greeter.prototype.greet = function () {
113 * alert('Hello ' + this.name + '!');
114 * }
115 * ```
116 *
117 * ### Example with parameter annotations
118 *
119 * ```
120 * var MyService = ng.Class({
121 * constructor: [String, [new Optional(), Service], function(name, myService) {
122 * ...
123 * }]
124 * });
125 * ```
126 *
127 * is equivalent to ES6:
128 *
129 * ```
130 * class MyService {
131 * constructor(name: string, @Optional() myService: Service) {
132 * ...
133 * }
134 * }
135 * ```
136 *
137 * ### Example with inheritance
138 *
139 * ```
140 * var Shape = ng.Class({
141 * constructor: (color) {
142 * this.color = color;
143 * }
144 * });
145 *
146 * var Square = ng.Class({
147 * extends: Shape,
148 * constructor: function(color, size) {
149 * Shape.call(this, color);
150 * this.size = size;
151 * }
152 * });
153 * ```
154 * @suppress {globalThis}
155 * @stable
156 */
157export declare function Class(this: any, clsDef: ClassDefinition): Type<any>;
158/**
159 * @suppress {globalThis}
160 */
161export declare function makeDecorator(name: string, props: {
162 [name: string]: any;
163}, parentClass?: any, chainFn?: (fn: Function) => void): (...args: any[]) => (cls: any) => any;
164export declare function makeParamDecorator(name: string, props: ([string, any] | {
165 [name: string]: any;
166})[], parentClass?: any): any;
167export declare function makePropDecorator(name: string, props: ([string, any] | {
168 [key: string]: any;
169})[], parentClass?: any): any;