Skip to content

Inconsistent type errors depending on order of statements #32209

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
WearyMonkey opened this issue Jul 2, 2019 · 1 comment · Fixed by #32225
Closed

Inconsistent type errors depending on order of statements #32209

WearyMonkey opened this issue Jul 2, 2019 · 1 comment · Fixed by #32225
Assignees
Labels
Bug A bug in TypeScript Fix Available A PR has been opened for this issue

Comments

@WearyMonkey
Copy link

TypeScript Version: 3.6.0-dev.20190701, 3.5.2 (Not present in 3.4.5)

Search Terms:

inconsistent error typing order interface

Code

type A = { a: number };
type B = { b: number };
type X<T> = ({ [K in keyof T]: T[K] } & Record<string, void>)[keyof T];
type P1<T> = { data: X<T> };
type P2<T> = { data: X<T> };

interface I<T> {
    fn<K extends keyof T>(p1: P1<Pick<T, K>>, p2: P2<Pick<T, K>>): void;
}

const i: I<A & B> = null as any;
const p2: P2<A> = null as any;

// Commenting out the below line will remove the error on the `const _i: I<A> = i;`
i.fn(null as any, p2);

const _i: I<A> = i;

The above code sample is a simplified version of real code in our code base. I tried simplifying it further but thats as far as I got.

Expected behavior:

The i.fn(null as any, p2); should have no effect on the typing of the const _i: I<A> = i; line.

Actual behavior:

When i.fn(null as any, p2); is present, there is an error on const _i: I<A> = i;,
When i.fn(null as any, p2); is removed OR moved after const _i: I<A> = i;, the error disappears.

Error:

test.ts:15:7 - error TS2322: Type 'I<A & B>' is not assignable to type 'I<A>'.
  Type 'A' is not assignable to type 'A & B'.
    Property 'b' is missing in type 'A' but required in type 'B'.

15 const _i: I<A> = i;
         ~~

  test.ts:2:12
    2 type B = { b: number };
                 ~
    'b' is declared here.


Found 1 error.

Playground Link:

I was not able to reproduce in the playground. But an empty compilerOptions and tsc will reliably reproduce the error in the listed versions.

v3.4.5 consistently provides no errors

Related Issues:

@RyanCavanaugh RyanCavanaugh added the Needs Investigation This issue needs a team member to investigate its status. label Jul 2, 2019
@RyanCavanaugh RyanCavanaugh added this to the TypeScript 3.6.0 milestone Jul 2, 2019
@weswigham weswigham added Bug A bug in TypeScript and removed Needs Investigation This issue needs a team member to investigate its status. labels Jul 2, 2019
@fatcerberus
Copy link

That's a weird one, an intervening function call causes the subsequent type check to become contravariant.

@weswigham weswigham added the Fix Available A PR has been opened for this issue label Jul 15, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug A bug in TypeScript Fix Available A PR has been opened for this issue
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants