-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(types): improve the
isArray
return type for unknown
input type (
#72)
- Loading branch information
1 parent
94543a4
commit ef14440
Showing
5 changed files
with
97 additions
and
11 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
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
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,2 +1,11 @@ | ||
export const isArray: (value: unknown) => value is readonly any[] = | ||
Array.isArray | ||
import type { ExtractNotAny } from 'radashi' | ||
|
||
export const isArray = Array.isArray as <Input>( | ||
value: Input, | ||
) => value is readonly any[] extends ExtractNotAny<Input, readonly any[]> | ||
? Extract<Input, readonly any[]> | ||
: any[] extends ExtractNotAny<Input, any[]> | ||
? Extract<Input, any[]> | ||
: unknown[] extends Input | ||
? unknown[] | ||
: 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,25 @@ | ||
declare const any: unique symbol | ||
|
||
/** | ||
* The `Any` class does not exist at runtime. It's used in type | ||
* definitions to detect an `any` type. | ||
* | ||
* ```ts | ||
* type IsAny<T> = [T] extends [Any] ? 'is any' : 'is not any' | ||
* ``` | ||
*/ | ||
export declare class Any { | ||
private any: typeof any | ||
} | ||
|
||
/** | ||
* Extracts `T` if `T` is not `any`, otherwise `never`. | ||
* | ||
* ```ts | ||
* type A = ExtractNotAny<any, string> | ||
* // ^? never | ||
* type B = ExtractNotAny<string | number, string> | ||
* // ^? string | ||
* ``` | ||
*/ | ||
export type ExtractNotAny<T, U> = Extract<[T] extends [Any] ? never : T, U> |
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,52 @@ | ||
import * as _ from 'radashi' | ||
|
||
describe('isArray return type', () => { | ||
test('value is any', () => { | ||
const value = {} as any | ||
if (_.isArray(value)) { | ||
expectTypeOf(value).toEqualTypeOf<unknown[]>() | ||
} else { | ||
expectTypeOf(value).toEqualTypeOf<any>() | ||
} | ||
}) | ||
test('value is unknown', () => { | ||
const value = {} as unknown | ||
if (_.isArray(value)) { | ||
expectTypeOf(value).toEqualTypeOf<unknown[]>() | ||
} else { | ||
expectTypeOf(value).toEqualTypeOf<unknown>() | ||
} | ||
}) | ||
test('value is string', () => { | ||
const value = {} as string | ||
if (_.isArray(value)) { | ||
expectTypeOf(value).toEqualTypeOf<never>() | ||
} else { | ||
expectTypeOf(value).toEqualTypeOf<string>() | ||
} | ||
}) | ||
test('value is string or ReadonlyMap', () => { | ||
const value = {} as string | readonly string[] | ||
if (_.isArray(value)) { | ||
expectTypeOf(value).toEqualTypeOf<readonly string[]>() | ||
} else { | ||
expectTypeOf(value).toEqualTypeOf<string>() | ||
} | ||
}) | ||
test('value is string, ReadonlyMap, or Map', () => { | ||
const value = {} as string | readonly string[] | string[] | ||
if (_.isArray(value)) { | ||
expectTypeOf(value).toEqualTypeOf<readonly string[] | string[]>() | ||
} else { | ||
expectTypeOf(value).toEqualTypeOf<string>() | ||
} | ||
}) | ||
test('value is string or Map', () => { | ||
const value = {} as string | string[] | ||
if (_.isArray(value)) { | ||
expectTypeOf(value).toEqualTypeOf<string[]>() | ||
} else { | ||
expectTypeOf(value).toEqualTypeOf<string>() | ||
} | ||
}) | ||
}) |