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

3.4 regression for type argument inference #30390

Closed
DanielRosenwasser opened this issue Mar 14, 2019 · 7 comments · Fixed by #45350
Closed

3.4 regression for type argument inference #30390

DanielRosenwasser opened this issue Mar 14, 2019 · 7 comments · Fixed by #45350
Assignees
Labels
Breaking Change Would introduce errors in existing code Fix Available A PR has been opened for this issue Needs Investigation This issue needs a team member to investigate its status. Rescheduled This issue was previously scheduled to an earlier milestone

Comments

@DanielRosenwasser
Copy link
Member

Found trying to compile VS Code at https://github.com/Microsoft/vscode/blob/1ce2b64abb08ed7998a5a6697ba0248a51934605/src/vs/workbench/services/textfile/common/textFileService.ts#L294-L338

Isolated repro:

function blah(): Promise<{ success: boolean }> {
    return Promise.resolve(100).then(() => {
        if (Math.random()) {
            return {
                success: true
            }
        }
        return Promise.resolve({ success: false })
    })
}
@DanielRosenwasser
Copy link
Member Author

DanielRosenwasser commented Mar 14, 2019

@DanielRosenwasser DanielRosenwasser changed the title 3.4 regression in flowing outer contextual types to inner contextually sensitive expressions(?) 3.4 regression for type argument inference Mar 14, 2019
@DanielRosenwasser
Copy link
Member Author

@mjbvz

@RyanCavanaugh
Copy link
Member

Error is

clipboard.ts:2     return Promise.resolve(100).then(() => {
                                                    ~~~~~~~
  TS2345: Argument of type '() => Promise<{ success: false; }> | { success: true; }' is not assignable to parameter of type '(value: number) => { success: false; } | PromiseLike<{ success: false; }>'.
    Type 'Promise<{ success: false; }> | { success: true; }' is not assignable to type '{ success: false; } | PromiseLike<{ success: false; }>'.
      Type '{ success: true; }' is not assignable to type '{ success: false; } | PromiseLike<{ success: false; }>'.
        Type '{ success: true; }' is not assignable to type '{ success: false; }'.
          Types of property 'success' are incompatible.
            Type 'true' is not assignable to type 'false'.

@weswigham
Copy link
Member

Ah, yes, the common bug of us inferring literal types where we really shouldn't.

@DanielRosenwasser
Copy link
Member Author

(thanks, I had to run to catch a bus)

@RyanCavanaugh RyanCavanaugh added the Needs Investigation This issue needs a team member to investigate its status. label Mar 25, 2019
@ahejlsberg
Copy link
Member

We now make inferences from generic return type positions (#29478), so the return statements have the contextual type { success: boolean } | PromiseLike<{ success: boolean }> where previously they had none. Because of the contextual type we infer more specific return expression types { success: true } and Promise<{ success: false }>, which in turn means we end up inferring both { success: true } and { success: false } for the TResult1 type variable of then. Neither of those is a subtype of the other, so we pick just one and subsequently end up with an error.

A simpler example of the same issue:

declare function capture<T>(f: () => [T]): [T];

let x1: [{ success: boolean }] = capture(() => {
    return Math.random() ? [{ success: true }] : [{ success: false }];
})

Interestingly, this very similar example doesn't have an issue:

let x2: [{ success: number }] = capture(() => {
    return Math.random() ? [{ success: 0 }] : [{ success: 1 }];
})

The reason is that a contextual type number doesn't cause us to infer literal types. Only when the contextual type itself contains literal types do we infer literal types. And that brings us to the core issue: For number and string we have a distinct non-literal type representing the entire domain, whereas boolean is indistinguishable from the union false | true (and the same is true for enum literal types).

We could attempt to track whether all occurrences of false and true in a contextual type originate in a reference to the boolean type and then not infer literal types, but it is non-trivial.

@RyanCavanaugh RyanCavanaugh added the Rescheduled This issue was previously scheduled to an earlier milestone label Aug 31, 2020
@MikeJerred
Copy link

I'm not sure if this is the same issue or not:

class Base {}

class Foo<T> extends Base {
  constructor(value: T, fn: (value: Foo<T>) => void) { super(); }
}

const foo = new Foo(5, (f: Foo<number>) => {});
// foo has type Foo<5>
const bar = new Foo(5, (f: Foo<number> & Base) => {});
// bar has type Foo<number>

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Breaking Change Would introduce errors in existing code Fix Available A PR has been opened for this issue Needs Investigation This issue needs a team member to investigate its status. Rescheduled This issue was previously scheduled to an earlier milestone
Projects
None yet
7 participants