-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* feat(MapKeys): Add MapKeys type * test(MapKeys): Add MapKeys type test * docs(MapKeys): Sort alphabetically * docs(MapKeys): Add MapKeys type documentation
- Loading branch information
1 parent
cec72c2
commit 41ecf7a
Showing
8 changed files
with
182 additions
and
66 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
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,32 @@ | ||
# MapKeys\<T> | ||
|
||
## 개요 | ||
|
||
주어진 Map 타입 `T`로 부터 키 타입을 추출해요. | ||
|
||
## 문법 | ||
|
||
```ts | ||
type MapKeys<T extends Map<unknown, unknown>> = | ||
T extends Map<infer K, unknown> ? K : never; | ||
``` | ||
|
||
- **T**: 키 타입을 추출할 Map 타입이에요. | ||
|
||
## 예제 | ||
|
||
```ts | ||
const phoneBook: Map<'tomas' | 'sha', string> = new Map([ | ||
['tomas': '010-0000-0000'], | ||
['sha': '010-1111-1111'], | ||
]) | ||
|
||
type PhoneBookKeys = MapKeys<typeof phoneBook>; // 'tomas' | 'sha' | ||
|
||
const hashMap: Map<string, string> = new Map([ | ||
['0': 'coffee'], | ||
['1': 'dessert'] | ||
]) | ||
|
||
type HashMapKeys = MapKeys<typeof hashMap>; // string | ||
``` |
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,32 @@ | ||
# MapKeys\<T> | ||
|
||
## Overview | ||
|
||
Extracts the key type from a given map type `T`. | ||
|
||
## Syntax | ||
|
||
```ts | ||
type MapKeys<T extends Map<unknown, unknown>> = | ||
T extends Map<infer K, unknown> ? K : never; | ||
``` | ||
|
||
- **T**: The map type from which to extract the key type. | ||
|
||
## Examples | ||
|
||
```ts | ||
const phoneBook: Map<'tomas' | 'sha', string> = new Map([ | ||
['tomas': '010-0000-0000'], | ||
['sha': '010-1111-1111'], | ||
]) | ||
|
||
type PhoneBookKeys = MapKeys<typeof phoneBook>; // 'tomas' | 'sha' | ||
|
||
const hashMap: Map<string, string> = new Map([ | ||
['0': 'coffee'], | ||
['1': 'dessert'] | ||
]) | ||
|
||
type HashMapKeys = MapKeys<typeof hashMap>; // string | ||
``` |
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,24 @@ | ||
/** | ||
* @description - Extracts the key type from a given map type `T`. | ||
* | ||
* @template T - The map type from which to extract the key type. | ||
* | ||
* @returns - The key type of `T`. | ||
* | ||
* @example | ||
* const phoneBook: Map<'tomas' | 'sha', string> = new Map([ | ||
* ['tomas': '010-0000-0000'], | ||
* ['sha': '010-1111-1111'], | ||
* ]) | ||
* | ||
* type PhoneBookKeys = MapKeys<typeof phoneBook>; // 'tomas' | 'sha' | ||
* | ||
* const hashMap: Map<string, string> = new Map([ | ||
* ['0': 'coffee'], | ||
* ['1': 'dessert'] | ||
* ]) | ||
* | ||
* type HashMapKeys = MapKeys<typeof hashMap>; // string | ||
*/ | ||
export type MapKeys<T extends Map<unknown, unknown>> = | ||
T extends Map<infer K, unknown> ? K : never; |
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 @@ | ||
export type { MapKeys } from './MapKeys'; |
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,18 @@ | ||
import { expectNever, expectType } from 'tsd'; | ||
import { MapKeys } from '@/map'; | ||
|
||
declare function mapKeys<T extends Map<unknown, unknown>>(): MapKeys<T>; | ||
|
||
// Should extract map keys correctly as 'tomas' | 'sha'. | ||
declare const phoneBook: Map<'tomas' | 'sha', string>; | ||
expectType<'tomas' | 'sha'>(mapKeys<typeof phoneBook>()); | ||
|
||
// Should extract map keys correctly as string. | ||
declare const hashMap: Map<string, string>; | ||
expectType<string>(mapKeys<typeof hashMap>()); | ||
|
||
// Should correctly infer `any` as the key type. | ||
expectType<any>(mapKeys<Map<any, string>>()); | ||
|
||
// Should not extract any keys and handle `never` type appropriately. | ||
expectNever(mapKeys<Map<never, string>>()); |