-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(types): ExtractSetType, ExtractMapType, Promiseable 추가 (#94)
- Loading branch information
Showing
8 changed files
with
60 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
'@modern-kit/types': minor | ||
--- | ||
|
||
feat(types): ExtractSetType, ExtractMapType, Promiseable 추가 |
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,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]>(); | ||
}); | ||
}); |
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,3 @@ | ||
export type ExtractMapType<T> = T extends Map<infer K, infer V> | ||
? [K, V] | ||
: 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,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[]>(); | ||
}); | ||
}); |
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 ExtractSetType<T> = T extends Set<infer U> ? U : 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,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>(); | ||
} | ||
}); | ||
}); |
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 Promiseable<T> = T | Promise<T>; |
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 |
---|---|---|
@@ -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'; |