-
Notifications
You must be signed in to change notification settings - Fork 25
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
cafc7fc
commit 9a4a97d
Showing
2 changed files
with
53 additions
and
2 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 |
---|---|---|
@@ -1,2 +1,9 @@ | ||
export const isArray: (value: unknown) => value is readonly any[] = | ||
Array.isArray | ||
export const isArray = Array.isArray as <Input>( | ||
value: Input, | ||
) => value is readonly any[] extends Extract<Input, readonly any[]> | ||
? Extract<Input, readonly any[]> | ||
: any[] extends Extract<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,44 @@ | ||
import * as _ from 'radashi' | ||
|
||
describe('isArray return type', () => { | ||
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>() | ||
} | ||
}) | ||
}) |