Skip to content

Commit 68962e8

Browse files
committed
docs(enum): fix wangdoc#85
1 parent 8ad5fc6 commit 68962e8

File tree

1 file changed

+3
-4
lines changed

1 file changed

+3
-4
lines changed

docs/enum.md

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -512,15 +512,14 @@ type Foo = keyof typeof MyEnum;
512512

513513
上面示例中,`keyof typeof MyEnum`可以取出`MyEnum`的所有成员名,所以类型`Foo`等同于联合类型`'A'|'B'`
514514

515-
注意,这里的`typeof`是必需的,否则`keyof MyEnum`相当于`keyof number`
515+
注意,这里的`typeof`是必需的,否则`keyof MyEnum`相当于`keyof string`
516516

517517
```typescript
518518
type Foo = keyof MyEnum;
519-
// "toString" | "toFixed" | "toExponential" |
520-
// "toPrecision" | "valueOf" | "toLocaleString"
519+
// number | typeof Symbol.iterator | "toString" | "charAt" | "charCodeAt" | ...
521520
```
522521

523-
上面示例中,类型`Foo`等于类型`number`的所有原生属性名组成的联合类型。
522+
上面示例中,类型`Foo`等于类型`string`的所有原生属性名组成的联合类型。这是`MyEnum`为字符串 Enum 的结果,如果`MyEnum`是数值 Enum,那么`keyof MyEnum`相当于`keyof number`
524523

525524
这是因为 Enum 作为类型,本质上属于`number``string`的一种变体,而`typeof MyEnum`会将`MyEnum`当作一个值处理,从而先其转为对象类型,就可以再用`keyof`运算符返回该对象的所有属性名。
526525

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy