Skip to content
This repository was archived by the owner on Oct 12, 2022. It is now read-only.

Commit 449d2d6

Browse files
committed
More discussion of 'any' and 'void'
1. Point out that 'any' disables checking whereas 'Object' does not. 2. Point out that you can define variables of type 'void' but they can only be assigned 'undefined'.
1 parent f6958b6 commit 449d2d6

File tree

1 file changed

+20
-4
lines changed

1 file changed

+20
-4
lines changed

pages/Basic Types.md

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ var c: Color = Color.Green;
9292
```
9393

9494
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:
9696

9797
```TypeScript
9898
enum Color {Red = 1, Green, Blue};
@@ -103,8 +103,8 @@ alert(colorName);
103103

104104
# Any
105105

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.
108108
In these cases, we want to opt-out of type-checking and let the values pass through compile-time checks.
109109
To do so, we label these with the `any` type:
110110

@@ -115,6 +115,16 @@ notSure = false; // okay, definitely a boolean
115115
```
116116

117117
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+
```
118128

119129
The `any` type is also handy if you know some part of the type, but perhaps not all of it.
120130
For example, you may have an array but the array has a mix of different types:
@@ -127,11 +137,17 @@ list[1] = 100;
127137

128138
# Void
129139

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.
131141
You may commonly see this as the return type of functions that do not return a value:
132142

133143
```TypeScript
134144
function warnUser(): void {
135145
alert("This is my warning message");
136146
}
137147
```
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

Comments
 (0)