Skip to content

Commit 604295c

Browse files
authored
Update typeGuard.md
1 parent 46cf80a commit 604295c

File tree

1 file changed

+8
-1
lines changed

1 file changed

+8
-1
lines changed

docs/types/typeGuard.md

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,12 @@
22
* [User Defined Type Guards](#user-defined-type-guards)
33

44
## Type Guard
5-
Type Guards allow you to narrow down the type of an object within a conditional block. TypeScript is aware of the usage of the JavaScript `instanceof` and `typeof` operators. If you use these in a conditional block, TypeScript will understand the type of the variable to be different within that conditional block. Here is a quick example where TypeScript realizes that a particular function does not exist on `string` and points out what was probably a user typo:
5+
Type Guards allow you to narrow down the type of an object within a conditional block.
6+
7+
8+
### typeof
9+
10+
TypeScript is aware of the usage of the JavaScript `instanceof` and `typeof` operators. If you use these in a conditional block, TypeScript will understand the type of the variable to be different within that conditional block. Here is a quick example where TypeScript realizes that a particular function does not exist on `string` and points out what was probably a user typo:
611

712
```ts
813
function doSomething(x: number | string) {
@@ -14,6 +19,8 @@ function doSomething(x: number | string) {
1419
}
1520
```
1621

22+
### instanceof
23+
1724
Here is an example with a class and `instanceof`:
1825

1926
```ts

0 commit comments

Comments
 (0)