简体   繁体   English

TypeScript:“A 扩展 B”的键入问题

[英]TypeScript: Typing problem with “A extends B”

[Edit - concluding remark]: It has turned out that the question below was mainly the result of some suboptimal typings and normally you might not have such issues like I had in my program => now with this knowledge, my question does not seem to make too much sense any more (as others have already mentioned below). [编辑 - 结束语]:事实证明,下面的问题主要是一些次优类型的结果,通常你可能没有像我在我的程序中遇到的这样的问题 => 现在有了这些知识,我的问题似乎没有不再有意义(正如其他人已经在下面提到的那样)。


Please have a look at the following TypeScript code snippet.请查看以下 TypeScript 代码片段。 As B extends A this c: 8 line is of course allowed.作为B extends A这个c: 8线当然是允许的。 How do I have to change the types of function f to forbid others keys next to a and b ?如何更改function f的类型以禁止ab旁边的其他键?

type A = { a: number, b: number }

function f<B extends A>(b: B): void {
}

f({
  a: 3,
  b: 4,
  c: 8 // <- This shall cause a compile error
})

» Demo » 演示

PS: Of course function f(b: A) {} is not what I am looking for. PS:当然function f(b: A) {}不是我要找的。 This example here is just simplified, the real world use-case is far more complicated... it's important that function f stays generic.这里的这个例子只是简化了,现实世界的用例要复杂得多……重要的是function f保持通用。


[Edit]: As my simplification above caused some confusion: Please find here a more complex example, where an anwer to my question above could help (an imaginary UI component API): [编辑]:由于我上面的简化引起了一些混乱:请在此处找到一个更复杂的示例,其中对我上面的问题的回答可能会有所帮助(一个虚构的 UI 组件 API):

Please have a look at the type error in Demo-A.请查看 Demo-A 中的类型错误。 This shall be fixed in Demo-B.这将在 Demo-B 中修复。

Demo-A (with type error) Demo-A(类型错误)

Demo-B (without type error) Demo-B(无类型错误)

But as you can see in Demo-B you can now add invalid parameters to the component configuration - which is not very nice... and that's why I have asked the question above.但是正如您在 Demo-B 中看到的那样,您现在可以将无效参数添加到组件配置中 - 这不是很好……这就是我提出上述问题的原因。

[Edit - later] Not an answer to my question above, but at least the following modification of Demo-B should work as desired: Demo-C [编辑-稍后]不是我上面问题的答案,但至少以下对 Demo-B 的修改应该可以按需要工作: Demo-C

The behavior is by design.行为是设计使然。 TS is structural type language where type compatibility is based on subtypying, in other words every type which has all constraints will pass the rule. TS 是结构类型语言,其中类型兼容性基于子类型化,换句话说,具有所有约束的每个类型都将通过规则。 In your example type of the value has all fields needed, so 'a' and 'b', therefore everything is ok.在您的示例中,值的类型具有所需的所有字段,因此“a”和“b”,因此一切正常。

There is no reason you should not allow for such object to be passed, as additional fields inside should not make you a difference.您没有理由不允许这样的 object 被传递,因为里面的其他字段不应该让您有所作为。 Saying that you extend means saying you are allowed to have more fields in terms of composites like objects.说你扩展意味着说你被允许在像对象这样的组合方面拥有更多的字段。

Additional if you want a subtype, but with exact fields the only type which matches your need will be unfortunately A , so the function signature should be f(a: A) .另外,如果您想要一个子类型,但使用确切的字段,不幸的是,唯一符合您需要的类型是A ,因此 function 签名应该是f(a: A)

And to be clear even with f(a: A) you will be able to pass values with additional fields, only object literals will be validated strictly.并且要清楚,即使使用f(a: A)您也可以传递带有其他字段的值,只有 object 文字将被严格验证。 Any assigned value with more fields can be used without compiler complain.任何具有更多字段的赋值都可以使用而不会编译器抱怨。 And the reason I describe already.以及我已经描述的原因。

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

相关问题 带有RxJS过滤器打字问题的打字稿 - Typescript with RxJS filter typing problem 与 Typescript 反应 - 键入 ChangeEvent 时出现问题 - React with Typescript - problem with typing ChangeEvent 如何:Typescript 嵌套打字 A <b<generic> > </b<generic> - How TO: Typescript nested typing A<B<GENERIC>> Typescript 接口 A 扩展了 B,但缺少 B 的属性? - Typescript interface A extends B, but is missing properties from B? TypeScript:键入自定义表单挂钩时出现问题 - TypeScript: Problem typing a cusom Form hook 键入自定义钩子 React/Typescript 时出现问题 - Problem with typing a custom hook React/Typescript 如何在 Typescript 中扩展另一个时声明 A 或 B 类型的变量 - How to declare variable of type A or B when one extends the other in Typescript TypeScript Generics 问题:类型 T(扩展类型)不可分配给 T - TypeScript Generics problem: type T (extends Type) is not assignable to T Typescript 错误? Function 接受 Object 定义为接受类型 A 和 B 的联合,其中 B 扩展 A - Typescript bug? Function accepts Object with missing prop when defined to accept union of type A and B where B extends A 打字稿泛型。 无法将B的实例传递给参数a:T,其中T扩展了B。为什么? - Typescript generics. Can't pass instance of B to a argument a: T where T extends B. Why?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM