Skip to content

Commit 908a8b0

Browse files
committed
complete ts chapter - 5
1 parent f03c355 commit 908a8b0

File tree

6 files changed

+132
-1
lines changed

6 files changed

+132
-1
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
| 2: Why and when to use TypeScript | [Readme](/chapters/2/readme.md) |
77
| 3: TypeScript Core Types | [Readme](/chapters/3/readme.md) |
88
| 4: Arrays | [Readme](/chapters/4/app.ts) |
9-
| 5: Enums | [Readme]() |
9+
| 5: Enums | [Readme](/chapters/5/readme.md) |
1010
| 6: Functions | [Readme]() |
1111
| 7: Classes | [Readme]() |
1212
| 8: Class Decorator | [Readme]() |

chapters/5/codes/5.1/app.ts

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/*
2+
By default all enum values are resolved to numbers. Let's say if you have something
3+
like
4+
5+
*/
6+
enum myEnum {
7+
JPEG,
8+
PNG,
9+
PDF
10+
}
11+
console.log(myEnum.PDF); // 2
12+
13+
enum myEnumLink {
14+
JPEG = <any>'image/jpeg',
15+
PNG = <any>'image/png',
16+
PDF = <any>'application/pdf'
17+
}
18+
console.log(myEnum.PNG); // 1
19+
20+
enum myEnumOnlyLink {
21+
JPEG = 'image/jpeg',
22+
PNG = 'image/png',
23+
PDF = 'application/pdf',
24+
}
25+
console.log(myEnum.JPEG); // 0
26+
27+
enum MyType {
28+
Value = 3,
29+
ValueEx = 30,
30+
ValueEx2 = 300
31+
}
32+
console.log(MyType.ValueEx); // 30
33+
34+
enum FancyType {
35+
OneArr = <any>[1],
36+
TwoArr = <any>[2, 2],
37+
ThreeArr = <any>[3, 3, 3]
38+
}
39+
console.log(FancyType.ThreeArr); // [ 3, 3, 3 ]

chapters/5/codes/5.2/app.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
enum SomeEnum { A, B };
2+
3+
console.log(SomeEnum); // { '0': 'A', '1': 'B', A: 0, B: 1 }
4+
5+
let enumValues:Array<string> = [];
6+
for(let value in SomeEnum) {
7+
if(typeof SomeEnum[value] === 'number') {
8+
enumValues.push(value);
9+
}
10+
}
11+
12+
console.log(enumValues); // [ 'A', 'B' ]
13+
14+
enumValues.forEach(v=> console.log(v));
15+
/*
16+
A
17+
B
18+
*/

chapters/5/codes/5.3/app.ts

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
enum SourceEnum {
2+
value1 = <any>'value1',
3+
value2 = <any>'value2'
4+
}
5+
6+
enum AdditionToSourceEnum {
7+
value3 = <any>'value3',
8+
value4 = <any>'value4'
9+
}
10+
11+
12+
// we need this type for TypeScript to resolve the types correctly
13+
type TestEnumType = SourceEnum | AdditionToSourceEnum;
14+
// and we need this value "instance" to use values
15+
let TestEnum = Object.assign({}, SourceEnum, AdditionToSourceEnum);
16+
// also works fine the TypeScript 2 feature
17+
// let TestEnum = { ...SourceEnum, ...AdditionToSourceEnum };
18+
19+
console.log(TestEnum);
20+
21+
function check(test: TestEnumType) {
22+
return test === TestEnum.value2;
23+
}
24+
25+
console.log(TestEnum.value1);
26+
console.log(TestEnum.value2 === <any>'value2');
27+
console.log(check(TestEnum.value2));
28+
console.log(check(TestEnum.value3));
29+
30+
/********
31+
* Some how there have a bit error
32+
* Object.assign not support there
33+
* **********/

chapters/5/codes/5.4/app.ts

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
class Enum {
2+
constructor(protected value: string) {}
3+
public toString() {
4+
return String(this.value);
5+
}
6+
7+
public is(value: Enum | string) {
8+
return this.value = value.toString();
9+
}
10+
}
11+
12+
class SourceEnum extends Enum {
13+
public static value1 = new SourceEnum('value1');
14+
public static value2 = new SourceEnum('value2');
15+
}
16+
17+
class TestEnum extends SourceEnum {
18+
public static value3 = new TestEnum('value3');
19+
public static value4 = new TestEnum('value4');
20+
}
21+
22+
function check(test: TestEnum) {
23+
return test === TestEnum.value2;
24+
}
25+
26+
let value1 = TestEnum.value1;
27+
28+
console.log(value1 + ' hello'); // value1 hello
29+
console.log(value1.toString() === 'value1'); // true
30+
console.log(value1.is('value1')); // value1
31+
console.log(!TestEnum.value3.is(TestEnum.value3)); // false
32+
console.log(check(TestEnum.value2)); // true
33+
console.log(TestEnum.value1 == <any>'value1'); // true

chapters/5/readme.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
# Enums
2+
3+
| **Topic** | **Code** |
4+
|:---------:|:--------:|
5+
| Section 5.1: Enums with explicit values | [code-here](/chapters/5/codes/5.1/app.ts) |
6+
| Section 5.2: How to get all enum values | [code-here](/chapters/5/codes/5.2/app.ts) |
7+
| Section 5.3: Extending enums without custom enum implementation | [code-here](/chapters/5/codes/5.3/app.ts) |
8+
| Section 5.4: Custom enum implementation: extends for enums | [code-here](/chapters/5/codes/5.4/app.ts) |

0 commit comments

Comments
 (0)