Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Can no longer pass Array.isArray to function accepting type guard to narrow type, e.g. array.filter(Array.isArray) #41610

Closed
felixfbecker opened this issue Nov 20, 2020 · 1 comment
Labels
Duplicate An existing issue was already created

Comments

@felixfbecker
Copy link
Contributor

TypeScript Version: 4.1.2

Search Terms:

Code

Playground

const aggregatedResults: (number[] | null | 'loading')[] = [[1, 2], null, [3, 4], 'loading', [5, 6]]
const onlyTheArrays = aggregatedResults.filter<number[]>(Array.isArray) // number[][]

making it easy to chain with more calls, e.g. .flat().
number[] needed to be passed so the end result was not any[], but it was type-safe, passing e.g. boolean[] would give an error.

Expected behavior:

Array.isArray as a native type guard function should be composable with type-guard accepting functions like array.filter(). Those functions need to be able to pluck the narrowed type out of the signature.

The above code worked in TS 4.0.

Actual behavior:

In TS 4.2 it seems like the signature of Array.isArray was changed and now has a conditional return type, which seems to make it impossible to pass it to a type-guard accepting function like array.filter(). The above code now gives an error (Playground):

Argument of type '<T>(arg: {} | T) => arg is T extends readonly any[] ? unknown extends T ? never : readonly any[] : any[]' is not assignable to parameter of type '(value: number[] | "loading" | null, index: number, array: (number[] | "loading" | null)[]) => value is number[]'.
  Type predicate 'arg is any[] | readonly any[]' is not assignable to 'value is number[]'.
    Type 'any[] | readonly any[]' is not assignable to type 'number[]'.
      The type 'readonly any[]' is 'readonly' and cannot be assigned to the mutable type 'number[]'.

array.filter() can't pluck the narrowed type out of the signature anymore because of the complex conditional type.

Related Issues: I think this was broken by #39258

@RyanCavanaugh
Copy link
Member

Evaluating isArray breaks at #41808

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Duplicate An existing issue was already created
Projects
None yet
Development

No branches or pull requests

2 participants