Skip to content

Commit

Permalink
feat(types): ExtractSetType, ExtractMapType, Promiseable 추가 (#94)
Browse files Browse the repository at this point in the history
  • Loading branch information
ssi02014 authored May 1, 2024
1 parent c6d7271 commit e4eba36
Show file tree
Hide file tree
Showing 8 changed files with 60 additions and 0 deletions.
5 changes: 5 additions & 0 deletions .changeset/fair-tips-smash.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@modern-kit/types': minor
---

feat(types): ExtractSetType, ExtractMapType, Promiseable 추가
15 changes: 15 additions & 0 deletions packages/types/src/ExtractMapType/ExtractMapType.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { ExtractMapType } from '.';

describe('ExtractMapType', () => {
it('should infer the generic type of a Map', () => {
const key = 'foo' as 'foo' | 'bar';
const value = 1 as 1 | 2 | 3;

const testMap = new Map<typeof key, typeof value>();

type extractedMapType = ExtractMapType<typeof testMap>; // ["foo" | "bar", 1 | 2 | 3]

expectTypeOf(key).toEqualTypeOf<extractedMapType[0]>();
expectTypeOf(value).toEqualTypeOf<extractedMapType[1]>();
});
});
3 changes: 3 additions & 0 deletions packages/types/src/ExtractMapType/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export type ExtractMapType<T> = T extends Map<infer K, infer V>
? [K, V]
: never;
13 changes: 13 additions & 0 deletions packages/types/src/ExtractSetType/ExtractSetType.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { ExtractSetType } from '.';

describe('ExtractSetType', () => {
it('should infer the generic type of a Set', () => {
const arr = [1, 2, 3, 4, 5] as const;
const testSet = new Set(arr);
const convertedTestArr = [...testSet];

type extractedSetType = ExtractSetType<typeof testSet>; // 1 | 2 | 3 | 4 | 5

expectTypeOf(convertedTestArr).toEqualTypeOf<extractedSetType[]>();
});
});
1 change: 1 addition & 0 deletions packages/types/src/ExtractSetType/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export type ExtractSetType<T> = T extends Set<infer U> ? U : never;
19 changes: 19 additions & 0 deletions packages/types/src/Promiseable/Promiseable.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { Promiseable } from '.';

describe('Promiseable', () => {
it('should correctly handle and type-check a variable as either Promise<number> or number when assigned with Promiseable<number>', () => {
const promise = Promise.resolve(1) as Promiseable<number>;

expectTypeOf(promise).toEqualTypeOf<Promise<number> | number>();
});

it('should correctly distinguish between Promise<number> and number type at runtime when using Promiseable<number>', () => {
const promise = Promise.resolve(1) as Promiseable<number>;

if (promise instanceof Promise) {
expectTypeOf(promise).toEqualTypeOf<Promise<number>>();
} else {
expectTypeOf(promise).toEqualTypeOf<number>();
}
});
});
1 change: 1 addition & 0 deletions packages/types/src/Promiseable/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export type Promiseable<T> = T | Promise<T>;
3 changes: 3 additions & 0 deletions packages/types/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
export * from './Arrayable';
export * from './ExcludeNullish';
export * from './ExtendOmittedProperties';
export * from './ExtractMapType';
export * from './ExtractSetType';
export * from './IndexSignature';
export * from './Nullable';
export * from './ObjectEntries';
export * from './ObjectKeys';
export * from './Promiseable';

0 comments on commit e4eba36

Please sign in to comment.