Skip to content

Commit d0e3459

Browse files
committed
feat: add quiz: type type type
1 parent 2230ed2 commit d0e3459

File tree

6 files changed

+129
-0
lines changed

6 files changed

+129
-0
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
**/.venv
2+
node_modules
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
# TypeTypeType
2+
3+
Author: [@yuta-ike](https://github.com/yuta-ike)
4+
5+
## 問題
6+
7+
a, b, c, d のうち、型エラーにならないものはどれ?
8+
9+
```ts
10+
let type = "type";
11+
type type<type> = type extends type ? type : typeof type;
12+
13+
const a: type<"type"> = type;
14+
const b: type<"type"> = "type";
15+
const c: type<"type"> = typeof type;
16+
const d: type<"type"> = typeof "type";
17+
```
18+
19+
[TypeScript Playground で確認する](https://www.typescriptlang.org/play/?#code/PTAEEMBpQI2hjaATUg7BkGIMhDBkIAMho9UBUMglwyA-DINYMgVgyCRDOYCIMgQQyqD2DIJYMgMQyD4-wFCcA2ApgBdQAgJ4AHPqAC8oAESiJsgNycFktQB41APmnDxkvgA8BfAHZIAzvomgA-DckAuRwHsAZo5Wd4rs5aFwF015A1ldGTUVX38hGGCDDVDFCLk1ZR8-ANB4BIkk9NS1Dy9M2NAkPL4CsKKDEuS+DKA)
20+
21+
<details>
22+
<summary>解答と解説</summary>
23+
24+
正解は b です。
25+
26+
`type` は JavaScript の予約語ではありません。TypeScript においても、変数名や型名、ジェネリクスの型変数名として使用可能です。
27+
28+
`type type<type> = type extends type ? type : typeof type;` をわかりやすく書き直すと以下のようになります。
29+
30+
```ts
31+
type X<T> = T extends T ? T : typeof T;
32+
```
33+
34+
さらに、T extends T は常に真なので、型 X は次のように簡略化できます。
35+
36+
```ts
37+
type X<T> = T;
38+
```
39+
40+
以上を踏まえて問題を分かりやすく書き直してみます。
41+
42+
```ts
43+
let y = "type";
44+
type X<T> = T;
45+
46+
const a: X<"type"> = y;
47+
const b: X<"type"> = "type";
48+
const c: X<"type"> = typeof y;
49+
const d: X<"type"> = typeof "type";
50+
```
51+
52+
ここで `X<"type">``"type"` というリテラル型に解決され、変数 y は let で宣言されているため string 型として扱われます。
53+
54+
以上を踏まえて各選択肢を確認します。
55+
56+
- a. `y`: y は string 型なので代入不可
57+
- b. `"type"`: リテラル型 "type" と一致するので代入可能
58+
- c. `typeof y`: typeof y は "string" | "number" | "bigint" | "boolean" | "symbol" | "undefined" | "object" | "function" に推論されるため代入不可
59+
- d. `typeof "type"`: typeof "type"は、c と同様に "string" | "number" | "bigint" | "boolean" | "symbol" | "undefined" | "object" | "function" に推論されるため代入不可
60+
61+
以上より、型エラーにならないのは b のみです。
62+
63+
</details>
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
let type = "type";
2+
type type<type> = type extends type ? type : typeof type;
3+
4+
const a: type<"type"> = type;
5+
const b: type<"type"> = "type";
6+
const c: type<"type"> = typeof type;
7+
const d: type<"type"> = typeof "type";

quiz/typescript/typetypetype/package-lock.json

Lines changed: 30 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"name": "typetypetype",
3+
"license": "MIT",
4+
"devDependencies": {
5+
"typescript": "^5.9.3"
6+
}
7+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
{
2+
"compilerOptions": {
3+
"module": "esnext",
4+
"target": "esnext",
5+
"noUncheckedIndexedAccess": true,
6+
"exactOptionalPropertyTypes": true,
7+
"noImplicitReturns": true,
8+
"noImplicitOverride": true,
9+
"noUnusedLocals": true,
10+
"noUnusedParameters": true,
11+
"noFallthroughCasesInSwitch": true,
12+
"noPropertyAccessFromIndexSignature": true,
13+
"strict": true,
14+
"verbatimModuleSyntax": true,
15+
"isolatedModules": true,
16+
"noUncheckedSideEffectImports": true,
17+
"moduleDetection": "force",
18+
"skipLibCheck": true,
19+
},
20+
"include": ["index.ts"]
21+
}

0 commit comments

Comments
 (0)