mattpocock/ts-reset

Type widening issue with `.filter(Boolean)`

Closed this issue · 5 comments

tjk commented

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

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

tjk commented

@mattpocock could you elaborate please?

Try it without ts-reset installed - same result.

tjk commented

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