Skip to content

Member-wise checks give incorrect errors for type assertions #55167

Open
@DanielRosenwasser

Description

@DanielRosenwasser

Playground Link

// No error. ✅
let okay = { id: 123, blah: "extra" } as { id: number };

// Errors. ❌
let waat = [{ id: 123, blah: "extra" }] as { id: number }[];
//                     ~~~~
// Conversion of type '{ id: number; blah: string; }[]' to type '{ id: number; }[]' may be a mistake because neither type sufficiently overlaps with the other. If this was intentional, convert the expression to 'unknown' first.
//   Type '{ id: number; blah: string; }' is not comparable to type '{ id: number; }'.
//     Object literal may only specify known properties, and 'blah' does not exist in type '{ id: number; }'.

Expected: Both of these type assertions should consistently apply the appropriate relationship check, and both should be free of errors.

Actual: The first assertion is free of errors, while the second has an excess property error.

Similar example I found over at https://github.com/microsoft/TypeScript/pull/55152/files#r1275497478:

let okay = { foo: "" } as { id: 123 };
//         ~~~~~~~~~~~~~~~~~~~~~~~~~~
// Conversion of type '{ foo: string; }' to type '{ id: 123; }' may be a mistake because neither type sufficiently overlaps with the other. If this was intentional, convert the expression to 'unknown' first.
//   Property 'id' is missing in type '{ foo: string; }' but required in type '{ id: 123; }'.

let waat = [{ foo: "" }] as { id: 123 }[];
//            ~~~
// Conversion of type '{ foo: string; }[]' to type '{ id: 123; }[]' may be a mistake because neither type sufficiently overlaps with the other. If this was intentional, convert the expression to 'unknown' first.
//   Type '{ foo: string; }' is not comparable to type '{ id: 123; }'.
//     Object literal may only specify known properties, and 'foo' does not exist in type '{ id: 123; }'.

Metadata

Metadata

Assignees

No one assigned

    Labels

    In DiscussionNot yet reached consensusNeeds InvestigationThis issue needs a team member to investigate its status.RescheduledThis issue was previously scheduled to an earlier milestoneSuggestionAn idea for TypeScript

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions