question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Allow non returning functions to have contextually required `undefined` return type

See original GitHub issue

Search Terms

#36239

Suggestion

Allow non returning functions to have contextually required undefined return type.

Use Cases

  • Callbacks

Examples

declare function f(a: () => undefined): void;
f(() => { }); // error -> ok
f((): undefined => { }); // error -> ok
const g1: () => undefined = () => { }; // error -> ok
const g2 = (): undefined => { }; // error -> ok if possible
function h1() {
}
f(h1); // error -> error
function h2(): undefined { // error -> ok if possible
}

Checklist

My suggestion meets these guidelines:

  • This wouldn’t be a breaking change in existing TypeScript/JavaScript code
  • This wouldn’t change the runtime behavior of existing JavaScript code
  • This could be implemented without emitting different JS based on the types of the expressions
  • This isn’t a runtime feature (e.g. library functionality, non-ECMAScript syntax with JavaScript output, etc.)
  • This feature would agree with the rest of TypeScript’s Design Goals.

Issue Analytics

  • State:open
  • Created 4 years ago
  • Reactions:34
  • Comments:12 (3 by maintainers)

github_iconTop GitHub Comments

20reactions
RyanCavanaughcommented, Jan 23, 2020

I like this idea 👍

5reactions
RyanCavanaughcommented, May 26, 2021

@ljwagerfield it sounds like you should turn on the noImplicitReturns flag, which does exactly that?

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to allow a function return type undefined - Stack Overflow
it says "A function whose declared type is neither 'void' nor 'any' must return a value". It must not. And everyone can verify...
Read more >
Void not throwing error when return value is specified - Reddit
The function type is supposed to return nothing as I used 'void' but it returns a string with no problems. No errors on...
Read more >
Functions - TypeScript: Handbook
Of note, only the parameters and the return type make up the function type. Captured variables are not reflected in the type. In...
Read more >
LLVM Language Reference Manual
The check function has no return value, but it will trigger an OS-level ... They are allowed in structs to facilitate intrinsics returning...
Read more >
4. Higher-Order Functions - Functional JavaScript [Book]
As it turned out, the need to take two functions (one to unwrap a value and ... returns undefined when an object does...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found