Skip to content

Filter predicate function with instanceOf issues on type inferenceΒ #51821

Closed
@jmoliugp

Description

@jmoliugp

Bug Report

πŸ”Ž Search Terms

instanceOf
predicate

πŸ•— Version & Regression Information

  • This error is appearing to me in versions 4.9.4, 4.8.2 & 4.4.4. Nothing makes me think it worked before in prior versions.

⏯ Playground Link

link

πŸ’» Code

const foo = () => {
  const a = 5
  const b = new Error('b')
  const c = new Error('c')
  const d = 'Hello world'
  const ls = [a, b, c, d]

  ls.filter<Error>(curr => curr instanceof Error)
  // Result: (2)Β [Error: b, Error: c]

  // Throws error:
  // Argument of type '(curr: string | number | Error) => boolean' is not assignable to parameter of type '(value: string | number | Error, index: number, array: (string | number | Error)[]) => value is Error'.
  // Signature '(curr: string | number | Error): boolean' must be a type predicate.ts(2345)
}

πŸ™ Actual behavior

It is neither recognizing the predicate function nor that the result will be an array of Error values.

πŸ™‚ Expected behavior

Interprets that the function is a predicate and filter the errors with the operator instanceOf.

Metadata

Metadata

Assignees

No one assigned

    Labels

    DuplicateAn existing issue was already created

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions