Skip to content

Don't widen return types of function expressions #241

Open
@RyanCavanaugh

Description

@RyanCavanaugh
Member

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

Activity

self-assigned this
on Jul 24, 2014
DanielRosenwasser

DanielRosenwasser commented on Mar 19, 2015

@DanielRosenwasser
Member

Bumping this since we ran into a situation with a leaked any in our own compiler. Minimal example:

function forEach<T, U>(array: T[], callback: (element: T, index: number) => U): U {
    if (array) {
        for (let i = 0, len = array.length; i < len; i++) {
            let result = callback(array[i], i);
            if (result) {
                return result;
            }
        }
    }
    return undefined;
}


forEach([1, 2, 3, 4], () => { 
    // do stuff
    if (blah) {
        // bam! accidental any
        return undefined;
    }
});
added
Needs ProposalThis issue needs a plan that clarifies the finer details of how it could be implemented.
and removed
Needs More InfoThe issue still hasn't been fully clarified
on Jul 27, 2015
mhegazy

mhegazy commented on Jul 27, 2015

@mhegazy
Contributor

We need an implementation proposal for this one.

JsonFreeman

JsonFreeman commented on Mar 22, 2016

@JsonFreeman
Contributor

What if we just did this:

  • Don't widen when you type a function body
  • When you widen a type, and it's a function type, you widen the return type
  • When you resolveName and it's a function expression or function declaration, you widen its type

139 remaining items

Loading
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Experimentation NeededSomeone needs to try this out to see what happensFix AvailableA PR has been opened for this issueSuggestionAn idea for TypeScript

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

      Participants

      @pelotom@jasonkuhrt@pronebird@DanielRosenwasser@mortoray

      Issue actions

        Don't widen return types of function expressions · Issue #241 · microsoft/TypeScript