Skip to content

Inconsistent type errors depending on order of statements #32209

@WearyMonkey

Description

@WearyMonkey

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:

Metadata

Metadata

Assignees

Labels

BugA bug in TypeScriptFix AvailableA PR has been opened for this issue

Type

No type
No fields configured for issues without a type.

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions