total-typescript / ts-reset

A 'CSS reset' for TypeScript, improving types for common JavaScript API's
https://www.totaltypescript.com/ts-reset
MIT License
7.74k stars 117 forks source link

Type widening issue with `.filter(Boolean)` #163

Closed tjk closed 11 months ago

tjk commented 11 months ago

Is this expected?

type A = {
  a: string
}

const as1: A[] = [
  { a: "1" },
  { a: "2", b: false },
         // ^ Type '{ a: string; b: false; }' is not assignable to type 'A'.
         //     Object literal may only specify known properties, and 'b' does not exist in type 'A'.
]

const as2: A[] = [
  { a: "1" },
  { a: "2", b: false }, // no error
].filter(Boolean)

TS Playground

mattpocock commented 11 months ago

Doesn't look like ts-reset is to blame here!

tjk commented 11 months ago

@mattpocock could you elaborate please?

mattpocock commented 11 months ago

Try it without ts-reset installed - same result.

tjk commented 11 months ago

But without ts-reset, the base .filter(Boolean) improvements also have same result.... I'm asking why this could not also be improved? We can both see it could do a better job here, right? Is this just not possible?

On Sun, Sep 3, 2023 at 1:01 AM Matt Pocock @.***> wrote:

Try it without ts-reset installed - same result.

— Reply to this email directly, view it on GitHub https://github.com/total-typescript/ts-reset/issues/163#issuecomment-1704042060, or unsubscribe https://github.com/notifications/unsubscribe-auth/AAA2VKAOWO27OE5O6LQYEA3XYQ2NZANCNFSM6AAAAAA3ZI44V4 . You are receiving this because you authored the thread.Message ID: @.***>

mattpocock commented 11 months ago

Right - no, it isn't, because of the way that TS treats excess property checking.