---
# System prepended metadata

title: TypeScript眉角
tags: [教學]

---

# TypeScript眉角

## 使用enum當作object key
```typescript=
enum testType {
    a = 1,
    b = 2,
    c = 3,
}
```
:::info
```typescript=
object : { [key in testType] : string}

object = {
    [testType.a] : "A",
    [testType.b] : "B",
    [testType.c] : "C",
}
```

object 的型別
```typescript
object : {
    1 : string,
    2 : string,
    3 : string,
}
```

```typescript
let data = object[testType.a];
//data 型別會變成 any
```
:::

:::success
上述例子的 object key 型別, 建議設成 number
```typescript
object : { [key : number] : string}
```
```typescript
let data = object[testType.a];
//data 型別是 string
```
:::
[參考來源](https://stackoverflow.com/a/52700831)

---

## 判斷某個未引用的外部class型別
:::info
```typescript=
class TestA {}

namespace ABC {
    class TestB {}
}
```
:::

:::success
```typescript
something instanceof (<any>window)["TestA"];
something instanceof (<any>window)["ABC"]["TestB"];
```
:::

---

###### tags: `教學`