Home > Enterprise >  In TS generics, why T != { foo: T['foo'] } & Omit<T, 'foo'>?
In TS generics, why T != { foo: T['foo'] } & Omit<T, 'foo'>?

Time:06-09

I have this code:

interface Base {
  id: string
}

interface Foo<T extends Base> {
    id: T['id'],
    rest: Omit<T, 'id'>
    check: (props: T) => void
}

function main <P extends Base>({ id, rest, check }: Foo<P>): void {
  check({
      id,
      ...rest
  })
}

And TS (v4.7.2) says that:

Argument of type '{ id: P["id"]; } & Omit<P, "id">' is not assignable to parameter of type 'P'.
  '{ id: P["id"]; } & Omit<P, "id">' is assignable to the constraint of type 'P', but 'P' could be instantiated with a different subtype of constraint 'Base'.(2345)

Each time when I had this error it was correct, but in this case, I can't understand the reason.

For me, this error sounds like (x - 1 1) != x.

Can anyone explain it? Thanks.

CodePudding user response:

It seems it's a known bug in TS

  • Related