From 244823e3a7d40323b87e9846b6ac272763ee6ec9 Mon Sep 17 00:00:00 2001 From: Alec Larson <1925840+aleclarson@users.noreply.github.com> Date: Tue, 9 Jul 2024 16:17:37 -0400 Subject: [PATCH] chore: tweak ExtractArray/Map/Set types --- src/typed/isArray.ts | 17 +++++++++++------ src/typed/isMap.ts | 16 ++++++++-------- src/typed/isSet.ts | 17 +++++++++++------ 3 files changed, 30 insertions(+), 20 deletions(-) diff --git a/src/typed/isArray.ts b/src/typed/isArray.ts index 9433de39..59545007 100644 --- a/src/typed/isArray.ts +++ b/src/typed/isArray.ts @@ -4,12 +4,17 @@ export const isArray = Array.isArray as ( value: Input, ) => value is ExtractArray -export type ExtractArray = Input extends any - ? [StrictExtract] extends [readonly any[]] - ? Extract - : [StrictExtract] extends [any[]] - ? Extract - : unknown[] extends Input +/** + * An absurdly complicated but accurate type for extracting Array types. + * + * It's like `Extract` but better with edge cases. + */ +export type ExtractArray = T extends any + ? [StrictExtract] extends [readonly any[]] + ? Extract + : [StrictExtract] extends [any[]] + ? Extract + : unknown[] extends T ? unknown[] : never : never diff --git a/src/typed/isMap.ts b/src/typed/isMap.ts index 06759a2d..ec08385e 100644 --- a/src/typed/isMap.ts +++ b/src/typed/isMap.ts @@ -6,17 +6,17 @@ export function isMap(value: Input): value is ExtractMap { /** * An absurdly complicated but accurate type for extracting Map types. + * + * It's like `Extract>` but better with edge cases. */ -export type ExtractMap = Input extends any - ? [StrictExtract>] extends [ +export type ExtractMap = T extends any + ? [StrictExtract>] extends [ ReadonlyMap, ] - ? Extract> - : [StrictExtract>] extends [ - Map, - ] - ? Extract> - : Map extends Input + ? Extract> + : [StrictExtract>] extends [Map] + ? Extract> + : Map extends T ? Map : never : never diff --git a/src/typed/isSet.ts b/src/typed/isSet.ts index b1159a32..b350cdd2 100644 --- a/src/typed/isSet.ts +++ b/src/typed/isSet.ts @@ -4,12 +4,17 @@ export function isSet(value: Input): value is ExtractSet { return isTagged(value, '[object Set]') } -export type ExtractSet = Input extends any - ? [StrictExtract>] extends [ReadonlySet] - ? Extract> - : [StrictExtract>] extends [Set] - ? Extract> - : Set extends Input +/** + * An absurdly complicated but accurate type for extracting Set types. + * + * It's like `Extract>` but better with edge cases. + */ +export type ExtractSet = T extends any + ? [StrictExtract>] extends [ReadonlySet] + ? Extract> + : [StrictExtract>] extends [Set] + ? Extract> + : Set extends T ? Set : never : never