-
Notifications
You must be signed in to change notification settings - Fork 100
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #858 from mikinovation/docs/add-no-infer-to-utilit…
…y-type NoInferのドキュメントを追加
- Loading branch information
Showing
6 changed files
with
56 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
--- | ||
description: 型推論を防ぐ | ||
title: NoInfer<T> | ||
--- | ||
|
||
`NoInfer<T>`は、`T`の型推論を防ぐためのユーティリティ型です。 | ||
|
||
## NoInfer<T>の型引数 | ||
|
||
### T | ||
|
||
型引数`T`には推論を防ぎたい型を代入します。 | ||
|
||
## NoInferの使用例 | ||
|
||
まずはNoInferを使用しなかった場合の例です。ジェネリクスを使って`getIndexFromArray`という関数を定義します | ||
|
||
```ts | ||
function getIndexFromArray<T extends string>(elements: T[], item: T): number { | ||
return elements.findIndex((element) => element === item); | ||
} | ||
|
||
type Fruit = "grape" | "apple" | "banana"; | ||
const fruits: Fruit[] = ["grape", "apple", "banana"]; | ||
getIndexFromArray(fruits, "apple"); | ||
getIndexFromArray(fruits, "peach"); | ||
``` | ||
|
||
このとき`T`は`"grape" | "apple" | "banana" | "peach"`と推論されてしまいます。しかし第二引数には配列に含まれている要素のみを許容したいです。そこで`NoInfer`を利用することにより`T`の型推論を防ぐことができます。 | ||
|
||
```ts twoslash | ||
function getIndexFromArray<T extends string>( | ||
elements: T[], | ||
item: NoInfer<T> | ||
): number { | ||
return elements.findIndex((element) => element === item); | ||
} | ||
|
||
type Fruit = "grape" | "apple" | "banana"; | ||
const fruits: Fruit[] = ["grape", "apple", "banana"]; | ||
getIndexFromArray(fruits, "apple"); | ||
// @errors: 2345 | ||
getIndexFromArray(fruits, "peach"); | ||
``` | ||
|
||
## 関連情報 | ||
|
||
[Infer](../../values-types-variables/type-inference.md) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters