Skip to content

Type-inference difference/regression between 1.5 and 1.6 #5969

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

Closed
suriya opened this issue Dec 7, 2015 · 1 comment
Closed

Type-inference difference/regression between 1.5 and 1.6 #5969

suriya opened this issue Dec 7, 2015 · 1 comment
Labels
Question An issue which isn't directly actionable in code

Comments

@suriya
Copy link

suriya commented Dec 7, 2015

I see a difference between how TypeScript 1.5 and 1.6 infer the type of function call. I am not sure if this is a bug. I would like to understand if this an indication of something subtle that users of the compiler must know. The shell output that follows illustrates how to reproduce this issue.

These are the two versions of the compiler that I am working with.

$ ./1.5.3/node_modules/.bin/tsc --version
message TS6029: Version 1.5.3
$ ./1.6.2/node_modules/.bin/tsc --version
message TS6029: Version 1.6.2

hello.ts has snippet from Definitely Typed's jquery.d.ts commit 1ba21f73e4104d567d64c03c89604e5960ae7f93

$ cat hello.ts 
/**
 * A few lines copied from the JQuery type definition to illustrate this
 * bug.
 * https://github.com/DefinitelyTyped/DefinitelyTyped/blob/1ba21f73e4104d567d64c03c89604e5960ae7f93/jquery/jquery.d.ts#L1547
 */
interface JQuery {
    // 1
    data(key: string, value: any): JQuery;
    // 2
    data(obj: { [key: string]: any; }): JQuery;
    // 3
    data(key: string): any;
    // 4
    data(): any;
};

function caller(jq: JQuery) {
    // We expect the data call below to call version 3 of the data function
    // mentioned above.
    var s: string = jq.data("hello");
}

TypeScript 1.5.3 and 1.6.2 compile the above file differently. 1.6.2 reports a compile error.

$ ./1.5.3/node_modules/.bin/tsc hello.ts # No error here
$ ./1.6.2/node_modules/.bin/tsc hello.ts
hello.ts(20,9): error TS2322: Type 'JQuery' is not assignable to type 'string'.

hello2.ts is a different snippet from Definitely Typed's jquery.d.ts commit 1d6c126e8ead25892ded5a32df2e813ffad06905. Note that hello2.ts and hello.ts differ in the order of function definitions within interface JQuery.

$ cat hello2.ts 
/**
 * A few lines copied from the JQuery type definition to illustrate this
 * bug.
 * https://github.com/DefinitelyTyped/DefinitelyTyped/blob/1d6c126e8ead25892ded5a32df2e813ffad06905/jquery/jquery.d.ts#L1547
 */
interface JQuery {
    // 1
    data(key: string, value: any): JQuery;
    // 3
    data(key: string): any;
    // 2
    data(obj: { [key: string]: any; }): JQuery;
    // 4
    data(): any;
};

function caller(jq: JQuery) {
    // We expect the data call below to call version 3 of the data function
    // mentioned above.
    var s: string = jq.data("hello");
}

Both compiler versions 1.5.3 and 1.6.2 don't report any error when compiling hello2.ts.

$ ./1.5.3/node_modules/.bin/tsc hello2.ts # No error
$ ./1.6.2/node_modules/.bin/tsc hello2.ts # No error

Is the difference in behavior between 1.5.3 and 1.6.2 a regression or an improvement? Why did Definitely Typed's file change the order of the definition of these functions? I could not figure this out fro m the commit message that made the change.

@ahejlsberg
Copy link
Member

In #4074 we fixed a bug that unfortunately was relied upon in overload resolution of calls to the jQuery function. We decided to keep the bug fix and also fix the unintended reliance on the buggy behavior in the jquery.d.ts definition file.

@ahejlsberg ahejlsberg added the Question An issue which isn't directly actionable in code label Dec 7, 2015
@microsoft microsoft locked and limited conversation to collaborators Jun 19, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Question An issue which isn't directly actionable in code
Projects
None yet
Development

No branches or pull requests

2 participants