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

Typescript can not recognize when expression is callable #44817

Closed
ZhenyaUsenko opened this issue Jun 29, 2021 · 4 comments
Closed

Typescript can not recognize when expression is callable #44817

ZhenyaUsenko opened this issue Jun 29, 2021 · 4 comments
Labels
Duplicate An existing issue was already created

Comments

@ZhenyaUsenko
Copy link

Bug Report

image

Quick fix

image

🔎 Search Terms

Not all constituents of type are callable.

💻 Code

const getValue = <V>(value: V | (() => V)) =>
  typeof value === "function" ? value() : value;

🙁 Actual behavior

Error occurred

🙂 Expected behavior

Typescript should understand that expression is callable

@MartinJohns
Copy link
Contributor

MartinJohns commented Jun 29, 2021

It would still not be safe, because V can be a function type.

Related / duplicate: #37663

@DanielRosenwasser DanielRosenwasser added the Duplicate An existing issue was already created label Jun 30, 2021
@ZhenyaUsenko
Copy link
Author

Yes, it can, but when typeof value === "function" returns true we are 100% sure value is callable

@MartinJohns
Copy link
Contributor

MartinJohns commented Jun 30, 2021

But you are not sure if it's callable without any arguments. V can be of the type (arg: number) => void as well.

@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