繁体   English   中英

在 TS 泛型中,为什么 T != { foo: T[&#39;foo&#39;] } &amp; Omit<T, 'foo'> ?

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

我有这个代码:

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
  })
}

TS (v4.7.2) 说:

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)

每次我遇到此错误时都是正确的,但在这种情况下,我无法理解原因。

对我来说,这个错误听起来像(x - 1 + 1) != x

谁能解释一下? 谢谢。

这似乎是 TS 中的一个已知错误

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM