You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
@@ -50,8 +47,6 @@ To learn how to use the original typescript compiler, please visit the following
50
47
| defines | Object || Replace the global variables with the constants defined in the "defines"" object. |
51
48
52
49
53
-
**Note: The "defines" option is only allowed in tsconfig.json, and not through command-line switches.**
54
-
55
50
Example tsconfig.json file:
56
51
57
52
```
@@ -65,7 +60,7 @@ Example tsconfig.json file:
65
60
"emitReflection": true,
66
61
"accessorOptimization": true,
67
62
"defines": {
68
-
"DEBUG": false, // the value can be boolean, number or string.
63
+
"DEBUG": false,
69
64
"RELEASE": true
70
65
}
71
66
@@ -76,4 +71,184 @@ Example tsconfig.json file:
76
71
]
77
72
}
78
73
79
-
```
74
+
```
75
+
76
+
## Reflection
77
+
78
+
Pass `--emitReflection` to the command-line tool or add `"emitReflection": true` to the `compilerOptions` in tsconfig.json file to enable this feature.
Pass `--accessorOptimization` to the command-line tool or add `"accessorOptimization": true` to the `compilerOptions` in tsconfig.json file to enable this feature.
127
+
128
+
As we know, we can't override the get / set accessors of super class in TypeScript. To solve the problem, we usually forward the call to the set accessor to another method:
129
+
130
+
TypeScript:
131
+
132
+
```
133
+
class Student {
134
+
135
+
public _name:string;
136
+
137
+
protected setName(value:string):void {
138
+
this._name = value;
139
+
}
140
+
141
+
public get name():string {
142
+
return this._name;
143
+
}
144
+
145
+
public set name(value:string) {
146
+
this.setName(value);
147
+
}
148
+
}
149
+
```
150
+
It does solve the problem, but also brings a performance issue, that two functions have to be called each time we call a set accessor. With the `--accessorOptimization` switch on, if the accessor contains only one call to another method, the compiler use that method to define accessor directly.
The `defines` option is only allowed in tsconfig.json, and not through command-line switches.
199
+
200
+
You can use the `defines` option to declare global variables that the compiler will assume to be constants (unless defined in scope). Then all the defined global variables will be replaced with the corresponding constants. For example:
201
+
202
+
tsconfig.json:
203
+
204
+
```
205
+
{
206
+
"compilerOptions": {
207
+
"defines": {
208
+
"DEBUG": false,
209
+
"LANGUAGE": "en_US"
210
+
}
211
+
}
212
+
}
213
+
214
+
```
215
+
TypeScript:
216
+
217
+
```
218
+
declare var DEBUG:boolean;
219
+
declare var LANGUAGE:string;
220
+
221
+
if (DEBUG) {
222
+
console.log("DEBUG is true");
223
+
}
224
+
225
+
console.log("The language is : " + LANGUAGE);
226
+
227
+
function someFunction():void {
228
+
let DEBUG = true;
229
+
if (DEBUG) {
230
+
console.log("DEBUG is true");
231
+
}
232
+
}
233
+
234
+
```
235
+
JavaScript:
236
+
237
+
```
238
+
if (false) {
239
+
console.log("DEBUG is true");
240
+
}
241
+
242
+
console.log("The language is : " + "en_US");
243
+
244
+
function someFunction() {
245
+
var DEBUG = true;
246
+
if (DEBUG) {
247
+
console.log("DEBUG is true");
248
+
}
249
+
}
250
+
```
251
+
As you can see, the second `if(DEBUG)` in `someFunction` is not replaced because it is defined in scope.
252
+
253
+
Note that the compiler does not dropping the unreachable code, because it is can be easily done by other tools like [UglifyJS](http://lisperator.net/uglifyjs/) or [Google Closure Compiler](https://developers.google.com/closure/compiler/).
0 commit comments