Skip to content

Filter predicate function with instanceOf issues on type inference #51821

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

Closed
jmoliugp opened this issue Dec 8, 2022 · 4 comments
Closed

Filter predicate function with instanceOf issues on type inference #51821

jmoliugp opened this issue Dec 8, 2022 · 4 comments
Labels
Duplicate An existing issue was already created

Comments

@jmoliugp
Copy link

jmoliugp commented Dec 8, 2022

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.

@MartinJohns
Copy link
Contributor

Duplicate of #38390.

Your function is not a type guard. Add a type annotation to make it a type guard and it will work.

@jmoliugp
Copy link
Author

jmoliugp commented Dec 8, 2022

Duplicate of #38390.

Your function is not a type guard. Add a type annotation to make it a type guard and it will work.

Can you try it on the playground and paste the code?

Tried it and still having the same problem. I'm not understanding how adding a type annotation would make a difference, the compiler infers the result of evaluating the expression it returns a boolean.

@MartinJohns
Copy link
Contributor

MartinJohns commented Dec 8, 2022

ls.filter<Error>((curr): curr is Error => curr instanceof Error)

I'm not understanding how adding a type annotation would make a difference, the compiler infers the result of evaluating the expression it returns a boolean.

The difference is, by adding a type annotation for the return type you don't let the compiler infer the result type at all, so it won't end up being boolean.

@RyanCavanaugh RyanCavanaugh added the Duplicate An existing issue was already created label Dec 8, 2022
@typescript-bot
Copy link
Collaborator

This issue has been marked as a 'Duplicate' and has seen no recent activity. It has been automatically closed for house-keeping purposes.

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

4 participants