Skip to content

Commit

Permalink
feat(MapKeys): Add MapKeys (#58)
Browse files Browse the repository at this point in the history
* feat(MapKeys): Add MapKeys type

* test(MapKeys): Add MapKeys type test

* docs(MapKeys): Sort alphabetically

* docs(MapKeys): Add MapKeys type documentation
  • Loading branch information
haejunejung authored Aug 29, 2024
1 parent cec72c2 commit 41ecf7a
Show file tree
Hide file tree
Showing 8 changed files with 182 additions and 66 deletions.
70 changes: 37 additions & 33 deletions docs/.vitepress/en.mts
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,43 @@ export default defineConfig({
{ text: 'Primitive', link: '/reference/basic/Primitive' },
],
},
{
text: 'Map',
items: [{ text: 'MapKeys', link: '/reference/map/MapKeys' }],
},
{
text: 'Predicate',
items: [
{ text: 'IsAny', link: '/reference/predicate/IsAny' },
{ text: 'IsArray', link: '/reference/predicate/IsArray' },
{ text: 'IsEqual', link: '/reference/predicate/IsEqual' },
{ text: 'IsTuple', link: '/reference/predicate/IsTuple' },
{ text: 'IsNever', link: '/reference/predicate/IsNever' },
{ text: 'IsUnknown', link: '/reference/predicate/IsUnknown' },
],
},
{
text: 'Special',
items: [{ text: 'Brand', link: '/reference/special/Brand' }],
},
{
text: 'String',
items: [
{ text: 'Replace', link: '/reference/string/Replace' },
{ text: 'ReplaceAll', link: '/reference/string/ReplaceAll' },
{ text: 'Split', link: '/reference/string/Split' },
{ text: 'WithPrefix', link: '/reference/string/WithPrefix' },
],
},
{
text: 'URL Parser',
items: [
{
text: 'ExtractRouteParams',
link: '/reference/url-parser/ExtractRouteParams',
},
],
},
{
text: 'Utilities',
items: [
Expand Down Expand Up @@ -75,39 +112,6 @@ export default defineConfig({
},
],
},
{
text: 'String',
items: [
{ text: 'Replace', link: '/reference/string/Replace' },
{ text: 'ReplaceAll', link: '/reference/string/ReplaceAll' },
{ text: 'Split', link: '/reference/string/Split' },
{ text: 'WithPrefix', link: '/reference/string/WithPrefix' },
],
},
{
text: 'Special',
items: [{ text: 'Brand', link: '/reference/special/Brand' }],
},
{
text: 'URL Parser',
items: [
{
text: 'ExtractRouteParams',
link: '/reference/url-parser/ExtractRouteParams',
},
],
},
{
text: 'Predicate',
items: [
{ text: 'IsAny', link: '/reference/predicate/IsAny' },
{ text: 'IsArray', link: '/reference/predicate/IsArray' },
{ text: 'IsEqual', link: '/reference/predicate/IsEqual' },
{ text: 'IsTuple', link: '/reference/predicate/IsTuple' },
{ text: 'IsNever', link: '/reference/predicate/IsNever' },
{ text: 'IsUnknown', link: '/reference/predicate/IsUnknown' },
],
},
],
},
],
Expand Down
70 changes: 37 additions & 33 deletions docs/.vitepress/ko.mts
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,43 @@ export default defineConfig({
{ text: 'Primitive', link: '/ko/reference/basic/Primitive' },
],
},
{
text: 'Map',
items: [{ text: 'MapKeys', link: '/ko/reference/map/MapKeys' }],
},
{
text: 'Predicate',
items: [
{ text: 'IsAny', link: '/ko/reference/predicate/IsAny' },
{ text: 'IsArray', link: '/ko/reference/predicate/IsArray' },
{ text: 'IsEqual', link: '/ko/reference/predicate/IsEqual' },
{ text: 'IsTuple', link: '/ko/reference/predicate/IsTuple' },
{ text: 'IsNever', link: '/ko/reference/predicate/IsNever' },
{ text: 'IsUnknown', link: '/ko/reference/predicate/IsUnknown' },
],
},
{
text: 'Special',
items: [{ text: 'Brand', link: '/ko/reference/special/Brand' }],
},
{
text: 'String',
items: [
{ text: 'Replace', link: '/ko/reference/string/Replace' },
{ text: 'ReplaceAll', link: '/ko/reference/string/ReplaceAll' },
{ text: 'Split', link: '/ko/reference/string/Split' },
{ text: 'WithPrefix', link: '/ko/reference/string/WithPrefix' },
],
},
{
text: 'URL Parser',
items: [
{
text: 'ExtractRouteParams',
link: '/ko/reference/url-parser/ExtractRouteParams',
},
],
},
{
text: 'Utilities',
items: [
Expand Down Expand Up @@ -78,39 +115,6 @@ export default defineConfig({
},
],
},
{
text: 'String',
items: [
{ text: 'Replace', link: '/ko/reference/string/Replace' },
{ text: 'ReplaceAll', link: '/ko/reference/string/ReplaceAll' },
{ text: 'Split', link: '/ko/reference/string/Split' },
{ text: 'WithPrefix', link: '/ko/reference/string/WithPrefix' },
],
},
{
text: 'Special',
items: [{ text: 'Brand', link: '/ko/reference/special/Brand' }],
},
{
text: 'URL Parser',
items: [
{
text: 'ExtractRouteParams',
link: '/ko/reference/url-parser/ExtractRouteParams',
},
],
},
{
text: 'Predicate',
items: [
{ text: 'IsAny', link: '/ko/reference/predicate/IsAny' },
{ text: 'IsArray', link: '/ko/reference/predicate/IsArray' },
{ text: 'IsEqual', link: '/ko/reference/predicate/IsEqual' },
{ text: 'IsTuple', link: '/ko/reference/predicate/IsTuple' },
{ text: 'IsNever', link: '/ko/reference/predicate/IsNever' },
{ text: 'IsUnknown', link: '/ko/reference/predicate/IsUnknown' },
],
},
],
},
],
Expand Down
32 changes: 32 additions & 0 deletions docs/ko/reference/map/MapKeys.md
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
```
32 changes: 32 additions & 0 deletions docs/reference/map/MapKeys.md
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
```
1 change: 1 addition & 0 deletions source/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
export * from './basic';
export * from './map';
export * from './predicate';
export * from './string';
export * from './special';
Expand Down
24 changes: 24 additions & 0 deletions source/map/MapKeys.d.ts
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;
1 change: 1 addition & 0 deletions source/map/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export type { MapKeys } from './MapKeys';
18 changes: 18 additions & 0 deletions test-d/map/MapKeys.test-d.ts
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>>());

0 comments on commit 41ecf7a

Please sign in to comment.