microsoft / microsoft/TypeScript
Member-wise checks give incorrect errors for type assertions
Nobody has claimed this yet.
- Dominant language
- Go
- Stars
- 111k
- Forks
- 14.3k
- Avg merge
- 1d 19h
- Merged PRs (30d)
- 117
Description
// 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; }'.
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start with the linked TypeScript Playground reproduction and compare the two type assertions, including the similar example from PR #55152. Trace the member-wise relationship check used for array assertions and add a regression test for the reported cases; done means both assertions consistently avoid the incorrect excess-property error.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- typescript
- Domain
- compilers
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Clearly specified
- Newbie friendliness
- 45/100