Skip to content

Confusing error position with new stricter type check and object spread operator #16473

Description

@yume-chan

TypeScript Version: 2.4.0

Code

interface A {
    a: string;
    b: string;
}

// A
const a0: A = {
    a: "a",
    b: "b",
    c: "c", // (1)
};

// B
const b1 = {
    a: "a",
    b: "b",
    c: "c", // (4)
};

const a1: A = { ...b1 /* (2) */ };

// C
interface B {
    a: string;
    b: string;
    c: string; // (5)
}

const b2: B = {
    a: "a",
    b: "b",
    c: "c",
};

const a2: A = { ...b2 /* (3) */ };

Expected behavior:

Type incompatible error at c: "c", // (1), ...b1 /* (2) */ and ...b2 /* (3) */

Type '{ a: string; b: string; c: string; }' is not assignable to type 'A'.
  Object literal may only specify known properties, and 'c' does not exist in type 'A'.

Actual behavior:

Error at c: "c", // (1), c: "c", // (4) and c: string; // (5). The later two are confusing.


I really don't know how can this check improve code quality. I can always assign the spread result to a temp local variable and then assign it to the final value, TypeScript doesn't care about this at all:

const b1 = {
    a: "a",
    b: "b",
    c: "c",
};

const a1: A = b1;

Metadata

Metadata

Assignees

Labels

BugA bug in TypeScriptFixedA PR has been merged 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