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.

Don't widen return types of function expressions

See original GitHub issue

This change courtesy @JsonFreeman who is trying it out

(context elided: widening of function expression return types)

The problem with this widening is observable in type argument inference and no implicit any. For type argument inference, we are prone to infer an any, where we should not:

function f<T>(x: T, y: T): T { }
f(1, null); // returns number
f(() => 1, () => null); // returns () => any, but should return () => number

So after we get to parity, I propose we do the following. We do not widen function expressions. A function is simply given a function type. However, a function declaration (and a named function expression) introduces a name whose type is the widened form of the type of the function. Very simple to explain and simple to implement.

I’ve been told that this would be a breaking change, because types that used to be any are now more specific types. But here are some reasons why it would be okay:

  • In the places where you actually need the type to be any (because there are no other inference candidates), you would still get any as a result
  • In places where there was a better (more specific) type to infer, you’d get the better type.
  • With the noImplicitAny flag, you’d get fewer errors because there are actually fewer implicit anys

Questions:

Is a principle of design changes going forward to not switch from ‘any’ to a more precise type because it can be a breaking change?

Going with ‘not a breaking change’ here because this is unlikely to break working code, but we need to verify this.

Would this manufacture two types?

In essence, we already have two types: The original and the widened type. So by that measure this is not really a change

Has someone tried it?

Jason willing to try it out and report back

Issue Analytics

  • State:open
  • Created 9 years ago
  • Reactions:77
  • Comments:30 (11 by maintainers)

github_iconTop GitHub Comments

26reactions
DanielRosenwassercommented, Sep 2, 2020

Hey everyone, I opened #40311 a couple of days ago which I think should fix most of the issues that have brought people here. If you’d like to give it a try, we have a build available over at https://github.com/microsoft/TypeScript/pull/40311#issuecomment-683255340.

19reactions
RyanCavanaughcommented, Sep 5, 2017

Time to take this back up since it’s affecting a lot of people writing reducers

Read more comments on GitHub >

github_iconTop Results From Across the Web

Don't Confuse Function Expressions and ... - Dmitri Pavlutin
In this post, you'll find how using the function keyword you can write function declarations and function expressions, and what are the ...
Read more >
How to declare Return Types for Functions in TypeScript
You are correct - here is a fully working example - you'll see that var result is implicitly a string because the return...
Read more >
Documentation - More on Functions - TypeScript
Function Type Expressions ... The syntax (a: string) => void means “a function with one parameter, named a , of type string, that...
Read more >
Functions :: Eloquent JavaScript
A return keyword without an expression after it will cause the function to return undefined . Functions that don't have a return statement...
Read more >
Extensions | Kotlin Documentation
Extensions do not actually modify the classes they extend. By defining an extension, you are not inserting new members into a class, only...
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