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

() => void 0 causes TS7011 with noImplicitAny #36651

Closed
avin-kavish opened this issue Feb 6, 2020 · 6 comments
Closed

() => void 0 causes TS7011 with noImplicitAny #36651

avin-kavish opened this issue Feb 6, 2020 · 6 comments
Labels
Question An issue which isn't directly actionable in code

Comments

@avin-kavish
Copy link

() => void 0

This function is guaranteed to return undefined under any and all conditions. Why is it saying that "Function expression, which lacks return-type annotation, implicitly has an 'any' return type"? When I look at this code, I see function expression, which lacks return-type annotation will always return undefined so why do I need to annotate this with types?

@JacksonKearl
Copy link

I can't reproduce this in playground https://www.typescriptlang.org/play/?ts=3.8-Beta#code/BQSgBAvAfGBuD2BLAJmADEA or VSCode, how are you seeing the error?

@avin-kavish
Copy link
Author

Web Storm. It's probably a web storm bug, web storm is also bugged on other typescript fronts. (It keeps auto-completing imports from @types/... and then telling me not to import from @types/... xD)

@ajafff
Copy link
Contributor

ajafff commented Feb 6, 2020

You need to enable strictNullChecks for this to work as expected.
Without strictNullChecks every type can contain undefined (and undefined itself is not considered a real type). Therefore TypeScript cannot infer which type you actually meant and infers any.

@avin-kavish
Copy link
Author

I see.

@RyanCavanaugh RyanCavanaugh added the Question An issue which isn't directly actionable in code label Feb 6, 2020
@typescript-bot
Copy link
Collaborator

This issue has been marked as 'Question' and has seen no recent activity. It has been automatically closed for house-keeping purposes. If you're still waiting on a response, questions are usually better suited to stackoverflow.

@lucattelli
Copy link

If you want to return undefined, you can simply write an empty arrow function. As functions that don't return nothing actually returns undefined.

const myFunction = () => { void 0 }
myFunction(); // undefined, but caught by TS7011

const MyNewFunction = () => { }
myNewFunction() // undefined, not caught by TS7011

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Question An issue which isn't directly actionable in code
Projects
None yet
Development

No branches or pull requests

6 participants