Skip to content

Commit

Permalink
Improve the typings of Array.isArray (#2552)
Browse files Browse the repository at this point in the history
  • Loading branch information
TylorS committed Apr 17, 2024
1 parent b2b5d66 commit 570e8d8
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 1 deletion.
5 changes: 5 additions & 0 deletions .changeset/slimy-dogs-warn.md
@@ -0,0 +1,5 @@
---
"effect": patch
---

Improve typings of Array.isArray
17 changes: 17 additions & 0 deletions packages/effect/dtslint/Array.ts
Expand Up @@ -19,10 +19,27 @@ declare const pimitiveNumber: number
declare const pimitiveNumerOrString: string | number
declare const predicateNumbersOrStrings: Predicate.Predicate<number | string>

declare const unknownValue: unknown
declare const stringOrStringArrayOrUnion: string | Array<string> | null

const symA = Symbol.for("a")
const symB = Symbol.for("b")
const symC = Symbol.for("c")

// -------------------------------------------------------------------------------------
// isArray
// -------------------------------------------------------------------------------------

if (Array.isArray(unknownValue)) {
// $ExpectType unknown[]
unknownValue
}

if (Array.isArray(stringOrStringArrayOrUnion)) {
// $ExpectType string[]
stringOrStringArrayOrUnion
}

// -------------------------------------------------------------------------------------
// isEmptyReadonlyArray
// -------------------------------------------------------------------------------------
Expand Down
5 changes: 4 additions & 1 deletion packages/effect/src/Array.ts
Expand Up @@ -345,7 +345,10 @@ export const scanRight: {
* @category guards
* @since 2.0.0
*/
export const isArray: (self: unknown) => self is Array<unknown> = Array.isArray
export const isArray: {
(self: unknown): self is Array<unknown>
<T>(self: T): self is Extract<T, ReadonlyArray<any>>
} = Array.isArray

/**
* Determine if an `Array` is empty narrowing down the type to `[]`.
Expand Down

0 comments on commit 570e8d8

Please sign in to comment.