mirror of
https://github.com/muerwre/muerwre.github.io.git
synced 2025-04-25 10:56:40 +07:00
added whole content
This commit is contained in:
parent
07a306a6f4
commit
dd104eed49
70 changed files with 5962 additions and 19 deletions
20
content/Typescript/Type guards.md
Normal file
20
content/Typescript/Type guards.md
Normal file
|
@ -0,0 +1,20 @@
|
|||
Useful for type checking at compile and run time:
|
||||
|
||||
```typescript
|
||||
function isFish(pet: Fish | Bird): pet is Fish {
|
||||
return (pet as Fish).swim !== undefined;
|
||||
}
|
||||
```
|
||||
|
||||
Usage:
|
||||
|
||||
```typescript
|
||||
const pet = getSmallPet();
|
||||
|
||||
if (isFish(pet)) {
|
||||
pet.swim();
|
||||
} else {
|
||||
pet.fly();
|
||||
}
|
||||
```
|
||||
|
Loading…
Add table
Add a link
Reference in a new issue