Skip to content

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

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

Open
DanielRosenwasser opened this issue Jul 26, 2023 · 2 comments
Open

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

DanielRosenwasser opened this issue Jul 26, 2023 · 2 comments
Labels
In Discussion Not yet reached consensus Needs Investigation This issue needs a team member to investigate its status. Rescheduled This issue was previously scheduled to an earlier milestone Suggestion An idea for TypeScript

Comments

@DanielRosenwasser
Copy link
Member

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; }'.
@DanielRosenwasser DanielRosenwasser changed the title Member-wise elaboration gives incorrect errors for casts Member-wise checks give incorrect errors for type assertions Jul 26, 2023
@DanielRosenwasser DanielRosenwasser added Suggestion An idea for TypeScript Needs Investigation This issue needs a team member to investigate its status. labels Jul 26, 2023
@DanielRosenwasser
Copy link
Member Author

DanielRosenwasser commented Jul 26, 2023

I'm guessing this behavior has probably been around for at least a few years, so realistically this issue is a suggestion that we loosen behavior that...maybe nobody finds objectionable.

That said, we now have satisfies. If you want the error, you should probably use that.

// Errors as expected! ❌
let okayA = { id: 123, blah: "extra" } satisfies { id: number };
//                     ~~~~

// Errors as expected! ❌
let okayB = [{ id: 123, blah: "extra" }] satisfies { id: number }[];
//                      ~~~~

Not sure what others think here.

@DanielRosenwasser DanielRosenwasser added the In Discussion Not yet reached consensus label Jul 26, 2023
@DanielRosenwasser DanielRosenwasser added this to the TypeScript 5.3.0 milestone Jul 26, 2023
@fatcerberus
Copy link

As a user, any case that involves an EPC error on a type assertion feels like a clear bug to me. EPC is designed as a linter-style check for catching wrong properties in object literals; a type assertion feels like a strong enough indicator of intent to rule out such mistakes (IOW the user is likely casting because the “wrong” properties are intended to be there).

I’d understand the team not wanting to take a breaking change on behavior that nobody has yet complained about, though.

@RyanCavanaugh RyanCavanaugh added the Rescheduled This issue was previously scheduled to an earlier milestone label Mar 4, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
In Discussion Not yet reached consensus Needs Investigation This issue needs a team member to investigate its status. Rescheduled This issue was previously scheduled to an earlier milestone Suggestion An idea for TypeScript
Projects
None yet
Development

No branches or pull requests

4 participants