Skip to content

Commit

Permalink
chore: tweak ExtractArray/Map/Set types
Browse files Browse the repository at this point in the history
  • Loading branch information
aleclarson committed Jul 9, 2024
1 parent 9257535 commit 244823e
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 20 deletions.
17 changes: 11 additions & 6 deletions src/typed/isArray.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,17 @@ export const isArray = Array.isArray as <Input>(
value: Input,
) => value is ExtractArray<Input>

export type ExtractArray<Input> = Input extends any
? [StrictExtract<Input, readonly any[]>] extends [readonly any[]]
? Extract<Input, readonly any[]>
: [StrictExtract<Input, any[]>] extends [any[]]
? Extract<Input, any[]>
: unknown[] extends Input
/**
* An absurdly complicated but accurate type for extracting Array types.
*
* It's like `Extract<T, any[]>` but better with edge cases.
*/
export type ExtractArray<T> = T extends any
? [StrictExtract<T, readonly any[]>] extends [readonly any[]]
? Extract<T, readonly any[]>
: [StrictExtract<T, any[]>] extends [any[]]
? Extract<T, any[]>
: unknown[] extends T
? unknown[]
: never
: never
16 changes: 8 additions & 8 deletions src/typed/isMap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,17 @@ export function isMap<Input>(value: Input): value is ExtractMap<Input> {

/**
* An absurdly complicated but accurate type for extracting Map types.
*
* It's like `Extract<T, Map<any, any>>` but better with edge cases.
*/
export type ExtractMap<Input> = Input extends any
? [StrictExtract<Input, ReadonlyMap<unknown, unknown>>] extends [
export type ExtractMap<T> = T extends any
? [StrictExtract<T, ReadonlyMap<unknown, unknown>>] extends [
ReadonlyMap<unknown, unknown>,
]
? Extract<Input, ReadonlyMap<unknown, unknown>>
: [StrictExtract<Input, Map<unknown, unknown>>] extends [
Map<unknown, unknown>,
]
? Extract<Input, Map<unknown, unknown>>
: Map<unknown, unknown> extends Input
? Extract<T, ReadonlyMap<unknown, unknown>>
: [StrictExtract<T, Map<unknown, unknown>>] extends [Map<unknown, unknown>]
? Extract<T, Map<unknown, unknown>>
: Map<unknown, unknown> extends T
? Map<unknown, unknown>
: never
: never
17 changes: 11 additions & 6 deletions src/typed/isSet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,17 @@ export function isSet<Input>(value: Input): value is ExtractSet<Input> {
return isTagged(value, '[object Set]')
}

export type ExtractSet<Input> = Input extends any
? [StrictExtract<Input, ReadonlySet<unknown>>] extends [ReadonlySet<unknown>]
? Extract<Input, ReadonlySet<unknown>>
: [StrictExtract<Input, Set<unknown>>] extends [Set<unknown>]
? Extract<Input, Set<unknown>>
: Set<unknown> extends Input
/**
* An absurdly complicated but accurate type for extracting Set types.
*
* It's like `Extract<T, Set<any>>` but better with edge cases.
*/
export type ExtractSet<T> = T extends any
? [StrictExtract<T, ReadonlySet<unknown>>] extends [ReadonlySet<unknown>]
? Extract<T, ReadonlySet<unknown>>
: [StrictExtract<T, Set<unknown>>] extends [Set<unknown>]
? Extract<T, Set<unknown>>
: Set<unknown> extends T
? Set<unknown>
: never
: never

0 comments on commit 244823e

Please sign in to comment.