|
1 | 1 | # Basic Types
|
2 | 2 |
|
3 |
| -## any |
4 |
| - |
5 |
| -You **should** avoid asserting type as `any`. |
6 |
| - |
7 |
| -> Why? |
8 |
| -
|
9 |
| -`as any` is a very powerful statement telling the compiler to turn off type checking. |
10 |
| - |
11 |
| -> Why not? |
12 |
| -
|
13 |
| -There are times that the compiler cannot infer the type correctly, |
14 |
| -or the type is specified by other libraries and is incorrect, |
15 |
| -or the type is too complicate to create and does not worth the effort to fix. |
16 |
| - |
17 |
| -For these cases, it is okey to use `as any`. |
18 |
| -Make sure you make this decision consciously. |
19 |
| - |
20 |
| ---- |
21 |
| - |
22 |
| -You **should** avoid annotating method parameters and callbacks as `any`. |
23 |
| - |
24 |
| -> Why? |
25 |
| -
|
26 |
| -Annotating function parameters and callbacks as `any` has similar effect as asserting them as `any`. |
27 |
| - |
28 |
| -The compiler might able to infer the type for you through inheritence or function type declaration. |
29 |
| -Annotating parameter as `any` overrides that and you loose all benefits of using TypeScript. |
30 |
| - |
31 |
| ---- |
32 |
| - |
33 |
| -You **may** use `as unknown`. |
34 |
| - |
35 |
| -### References |
36 |
| - |
37 |
| -- <https://github.com/Microsoft/TypeScript/pull/24439> |
38 |
| -- <https://mariusschulz.com/blog/the-unknown-type-in-typescript> |
39 |
| - |
40 |
| -## enum |
41 |
| - |
42 |
| -> Enums allow us to define a set of named constants. |
43 |
| -
|
44 |
| -- <https://www.typescriptlang.org/docs/handbook/enums.html> |
45 |
| -- <https://www.typescriptlang.org/docs/handbook/basic-types.html#enum> |
46 |
| - |
47 |
| -There are a few special rules about `enum` type: |
48 |
| - |
49 |
| -- values can be either `string` or `number` |
50 |
| - |
51 |
| -```ts |
52 |
| -enum map { a = 1, b = 'b' } |
53 |
| -} |
54 |
| -``` |
55 |
| - |
56 |
| -- it is nominal typed |
57 |
| - |
58 |
| -```ts |
59 |
| -enum JapanVocaloid { miku, luka } |
60 |
| - |
61 |
| -enum ChinaVocaloid { miku, LuoTianyi, XingChen } |
62 |
| - |
63 |
| -let myFavoriteVocaloid = ChinaVocaloid.miku |
64 |
| - |
65 |
| -// error |
66 |
| -myFavoriteVocaloid = JapanVocaloid.miku |
67 |
| -``` |
68 |
| - |
69 |
| ---- |
70 |
| - |
71 |
| -You **should avoid** using `enum`. You **should** use `const` instead. |
72 |
| - |
73 |
| -```ts |
74 |
| -// bad |
75 |
| -enum Color { Red, Green, Blue } |
76 |
| - |
77 |
| -// good |
78 |
| -const COLOR = { |
79 |
| - Red: 0, |
80 |
| - Green: 1, |
81 |
| - Blue: 2 |
82 |
| -} as const |
83 |
| - |
84 |
| -// good |
85 |
| -const COLOR_RED = 0 |
86 |
| -const COLOR_GREEN = 1 |
87 |
| -const COLOR_BLUE = 2 |
88 |
| -``` |
89 |
| - |
90 |
| -> Why? |
91 |
| -
|
92 |
| -While `enum` is more compact, |
93 |
| -and provide additional nominal protection, |
94 |
| -it is a TypeScript syntax. |
95 |
| -It goes against our design principles. |
96 |
| - |
97 |
| -The transpiled code can be different depends on the config options, |
98 |
| -and it can get confusing as it may get erased (`const enum`). |
| 3 | +- [`any`](./any.md) |
| 4 | +- [`unknown`](./unknown.md) |
| 5 | +- [`enum`](./enum.md) |
99 | 6 |
|
100 | 7 | ## References
|
101 | 8 |
|
|
0 commit comments