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

Don't widen return types of function expressions #241

Open
RyanCavanaugh opened this Issue Jul 24, 2014 · 10 comments

Comments

9 participants
@RyanCavanaugh
Copy link
Member

RyanCavanaugh commented Jul 24, 2014

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

@DanielRosenwasser

This comment has been minimized.

Copy link
Member

DanielRosenwasser commented Mar 19, 2015

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;
    }
});
@mhegazy

This comment has been minimized.

Copy link
Contributor

mhegazy commented Jul 27, 2015

We need an implementation proposal for this one.

@JsonFreeman

This comment has been minimized.

Copy link
Contributor

JsonFreeman commented Mar 22, 2016

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
@myitcv

This comment has been minimized.

Copy link

myitcv commented Apr 8, 2016

I've arrived here via #7220. Would appreciate some guidance on whether the following is expected behaviour or not (playground):

interface G<K, R> {
    (v: K): R;
}

function F<T>(v: T): T {
    return v;
}

interface A {
    __Type: "A";
}

interface B {
    __Type: "B";
}

let a: A;
let b: B;

a = b; // OK: error as expected, B is not assignable to A
b = a; // OK: error as expected, A is not assignable to B

let x: G<A, B> = F;  // How is this possible?

It's the final assignment I cannot understand... because the compiler does not complain.

@RyanCavanaugh

This comment has been minimized.

Copy link
Member Author

RyanCavanaugh commented Apr 8, 2016

During assignment checking, type parameters are replaced with any. So the effective signature of F here is (a: any) => any

@JsonFreeman

This comment has been minimized.

Copy link
Contributor

JsonFreeman commented Apr 8, 2016

@RyanCavanaugh

This comment has been minimized.

Copy link
Member Author

RyanCavanaugh commented Sep 5, 2017

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

@pelotom

This comment has been minimized.

Copy link

pelotom commented Nov 29, 2017

Any updated status on this?

@aczekajski

This comment has been minimized.

Copy link

aczekajski commented Dec 18, 2017

bump

@grantila

This comment has been minimized.

Copy link

grantila commented Dec 23, 2017

I came here by Daniel's reference from #20859. I'm having trouble writing typesafe libraries where users can provide callbacks which should follow the type contract of the library function.

And, as shown in my examples in #20859, this affects not only generic funtions (as depicted here) but also non-generic strictly typed functions. Basically, the return of a callback function is type unsafe, always, since the callback is a variable, and any assigning of a function to a variable (of a function type) ignores the return value of the r-value.

A lot of safety is missed by the current behavior.

@mhegazy mhegazy modified the milestones: TypeScript 2.7, TypeScript 2.8 Jan 9, 2018

@weswigham weswigham added this to In Review in Rolling Work Tracking Mar 3, 2018

@mhegazy mhegazy modified the milestones: TypeScript 2.8, Future Mar 9, 2018

@JasonGore JasonGore referenced this issue Jan 17, 2019

Merged

Foundation: Styles/Tokens Typing Improvements #7711

2 of 2 tasks complete
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.