Skip to content

Commit 85cc8ff

Browse files
authored
Update enums.md
1 parent 0404140 commit 85cc8ff

File tree

1 file changed

+5
-3
lines changed

1 file changed

+5
-3
lines changed

docs/enums.md

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -114,14 +114,16 @@ enum AnimalFlags {
114114
Here we are using the left shift operator to move `1` around a certain level of bits to come up with bitwise disjoint numbers `0001`, `0010`, `0100` and `1000` (these are decimals `1`,`2`,`4`,`8` if you are curious). The bitwise operators `|` (or) / `&` (and) / `~` (not) are your best friends when working with flags and are demonstrated below:
115115

116116
```ts
117-
118117
enum AnimalFlags {
119118
None = 0,
120119
HasClaws = 1 << 0,
121120
CanFly = 1 << 1,
122121
}
122+
type Animal = {
123+
flags: AnimalFlags
124+
}
123125

124-
function printAnimalAbilities(animal) {
126+
function printAnimalAbilities(animal: Animal) {
125127
var animalFlags = animal.flags;
126128
if (animalFlags & AnimalFlags.HasClaws) {
127129
console.log('animal has claws');
@@ -134,7 +136,7 @@ function printAnimalAbilities(animal) {
134136
}
135137
}
136138

137-
var animal = { flags: AnimalFlags.None };
139+
let animal: Animal = { flags: AnimalFlags.None };
138140
printAnimalAbilities(animal); // nothing
139141
animal.flags |= AnimalFlags.HasClaws;
140142
printAnimalAbilities(animal); // animal has claws

0 commit comments

Comments
 (0)