Skip to content

Type predicate using generic not working implicitly - Signature must be a type predicate #55641

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
jroru opened this issue Sep 5, 2023 · 3 comments
Labels
Duplicate An existing issue was already created

Comments

@jroru
Copy link

jroru commented Sep 5, 2023

πŸ”Ž Search Terms

  • type predicate
  • generic
  • must be a type predicate

πŸ•— Version & Regression Information

  • TS 5.2.2
  • This is the behavior in every version I tried

⏯ Playground Link

https://www.typescriptlang.org/play?ts=5.2.2#code/MYewdgzgLgBAbgQwDYEsAmCogE4DEHBbYCeMAvDADwAqANAHwAUAUDPMupjgFwyPAIwaTlACmvAK5gA1mBAB3MAEpy9GAKEjRMFBBjVmKsmoDeAXwDczZgHobMUQA8ADqMKi0MALQxsoqBLYYDBQxK4wIABm7KgYRA4uqMAoUEikztggcOgezIixXHgERMSUYBIAtgBGothMrHwawnGiSrxNWjp65dW1qiFholHqgs2Y2mSTMABEPTXY04ZWtvZysE6u7p4+fgFBA+HD+SI4MBAAFiASSJ6gFdqRmRUh59oA5qJgtSjAMM4I2AQz0YogAdG9QTA5rUVBksjlPOAYKgvjAAIxovIcOI4fCEHClaF1FhsfijLRGNShVzDDotchTWaVeaLJQWIA

πŸ’» Code

const validatorFactory = <T,>(
  validator: (candidate: unknown) => candidate is T
) => {};

// expected - return type of validator explicitly provided
validatorFactory<number>(
  (candidate): candidate is number => typeof candidate === "number"
);

// not expected - return type of validator should come from the generic param (e.g. number)
validatorFactory<number>(
  (candidate) => typeof candidate === "number"
);

πŸ™ Actual behavior

Argument of type '(candidate: unknown) => boolean' is not assignable to parameter of type '(candidate: unknown) => candidate is number'.
  Signature '(candidate: unknown): boolean' must be a type predicate.(2345)

πŸ™‚ Expected behavior

The return type of the type predicate should be based on the generic parameter implicitly.

Additional information about the issue

No response

@MartinJohns
Copy link
Contributor

Related / duplicate of #38390.

Arrow functions are never inferred as type guards.

@jroru jroru changed the title Type predicate using generic not working implicitly - Signature '(candidate: unknown): boolean' must be a type predicate Type predicate using generic not working implicitly - Signature must be a type predicate Sep 5, 2023
@jroru
Copy link
Author

jroru commented Sep 6, 2023

Is this achievable somehow without using arrow functions?

@andrewbranch andrewbranch added the Duplicate An existing issue was already created label Sep 7, 2023
@typescript-bot
Copy link
Collaborator

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

@typescript-bot typescript-bot closed this as not planned Won't fix, can't repro, duplicate, stale Sep 10, 2023
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