Allow non returning functions to have contextually required `undefined` return type
See original GitHub issueSearch Terms
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:
- Created 4 years ago
- Reactions:34
- Comments:12 (3 by maintainers)
Top 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 >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
I like this idea 👍
@ljwagerfield it sounds like you should turn on the
noImplicitReturns
flag, which does exactly that?