@@ -92,7 +92,7 @@ var c: Color = Color.Green;
92
92
```
93
93
94
94
A handy feature of enums is that you can also go from a numeric value to the name of that value in the enum.
95
- For example, if we had the value ` 2 ` but weren't sure which that mapped to in the ` Color ` enum above, we could look up the corresponding name:
95
+ For example, if we had the value ` 2 ` but weren't sure what that mapped to in the ` Color ` enum above, we could look up the corresponding name:
96
96
97
97
``` TypeScript
98
98
enum Color {Red = 1 , Green , Blue };
@@ -103,8 +103,8 @@ alert(colorName);
103
103
104
104
# Any
105
105
106
- We may need to describe the type of variables that we may not know when we are writing the application.
107
- These values may come from dynamic content, e.g. from the user or 3rd party library.
106
+ We may need to describe the type of variables that we do not know when we are writing an application.
107
+ These values may come from dynamic content, e.g. from the user or a 3rd party library.
108
108
In these cases, we want to opt-out of type-checking and let the values pass through compile-time checks.
109
109
To do so, we label these with the ` any ` type:
110
110
@@ -115,6 +115,16 @@ notSure = false; // okay, definitely a boolean
115
115
```
116
116
117
117
The ` any ` type is a powerful way to work with existing JavaScript, allowing you to gradually opt-in and opt-out of type-checking during compilation.
118
+ You might expect ` Object ` to play a similar role, as it does in other languages.
119
+ But variables of type ` Object ` only allow you to assign any value to them -- you can't call arbitrary methods on them, even ones that actually exist:
120
+
121
+ ``` TypeScript
122
+ var notSure: any = 4 ;
123
+ notSure .ifItExists (); // okay, ifItExists might exist at runtime
124
+ notSure .toFixed (); // okay, toFixed exists (but the compiler doesn't check)
125
+ var prettySure: Object = 4 ;
126
+ prettySure .toFixed (); // Error: Property 'toFixed' doesn't exist on type 'Object'.
127
+ ```
118
128
119
129
The ` any ` type is also handy if you know some part of the type, but perhaps not all of it.
120
130
For example, you may have an array but the array has a mix of different types:
@@ -127,11 +137,17 @@ list[1] = 100;
127
137
128
138
# Void
129
139
130
- Perhaps the opposite in some ways to ` any ` is ` void ` , the absence of having any type at all.
140
+ ` void ` is a little like the opposite of ` any ` : the absence of having any type at all.
131
141
You may commonly see this as the return type of functions that do not return a value:
132
142
133
143
``` TypeScript
134
144
function warnUser(): void {
135
145
alert (" This is my warning message" );
136
146
}
137
147
```
148
+
149
+ Declaring variables of type ` void ` is not useful because you can only assign ` undefined ` or ` null ` to them:
150
+
151
+ ``` TypeScript
152
+ var unusable: void = undefined ;
153
+ ```
0 commit comments