简体   繁体   English

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

This might be a bug or me not understanding union types.这可能是一个错误或者我不理解联合类型。 Take a look at this code:看看这段代码:

TS Playground TS游乐场

interface A {
  a?: string;
  b?: boolean;
}

interface B extends A{
  x: boolean;
  y: string;
}

function printCoord(pt: A | B) {
  console.log("The coordinate's x value is " + pt.a);
  console.log("The coordinate's y value is " + pt.b);
}
printCoord({ a: "asdf", b: true });

printCoord({a: "asdf", x: false});

The last line should not compile, because the presence of x enforces type B , but y is missing so it should actually be invalid.最后一行不应该编译,因为x的存在强制类型B ,但y缺失,所以它实际上应该是无效的。

The union type is used to declare a type that is either one (or even both). union 类型用于声明一种类型,它可以是其中之一(甚至两者都是)。

example:例子:

interface A {
  a1: string;
  a2: string;
}

interface B {
  b1: string;
  b2: string;
}

const e1: A|B = {a1: "1", a2:"1"}  // valid
const e2: A|B = {b1: "1", b2:"1"}  // valid
const e3: A|B = {a1: "1", b1:"1"}  // invalid

In your example, you are trying to assign {a: "asdf", x: false} to A | B在您的示例中,您尝试将{a: "asdf", x: false}分配给A | B A | B . A | B since type A accepts pretty much anything (even {} ) then {a: "asdf", x: false} can be assigned to A | B因为类型A几乎可以接受任何东西(甚至{} ),所以{a: "asdf", x: false}可以分配给A | B A | B . A | B

The reason it doesn't throw an error about the fact that there is a B property there is because A | B它没有抛出关于存在B属性的错误的原因是因为A | B A | B allows the value to be a mix of both. A | B允许值是两者的混合。 It has to be at least A or B, but can "inspire" to be both.它必须至少是 A 或 B,但可以“激发”两者兼而有之。

that's why this is also valid这就是为什么这也是有效的

printCoord({ a: "asdf" });  // valid

暂无
暂无

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

相关问题 如何在 Typescript 中扩展另一个时声明 A 或 B 类型的变量 - How to declare variable of type A or B when one extends the other in Typescript Typescript 接口 A 扩展了 B,但缺少 B 的属性? - Typescript interface A extends B, but is missing properties from B? 如何为React props定义TypeScript类型,只有在将prop A传递给组件时,才会接受prop B? - How do I define a TypeScript type for React props where prop B is only accepted if prop A is passed to a component? 当 Type B 以某种方式“扩展”Type A 时,如何停止 TypeScript 错误“Type A 没有与 Type B 相同的属性” - How to stop TypeScript error 'Type A has no properties in common with Type B' when Type B "extends" Type A in some way Typescript 在联合类型中通过参数 b 获取参数 a - Typescript get param a by param b in an union type TypeScript:“A 扩展 B”的键入问题 - TypeScript: Typing problem with “A extends B” Typescript 有条件的 Object 键:存在 Prop A 存在 Prob B - Typescript Conditional Object Keys: Prop A exists Prob B must exist 打字稿泛型。 无法将B的实例传递给参数a:T,其中T扩展了B。为什么? - Typescript generics. Can't pass instance of B to a argument a: T where T extends B. Why? Typescript 联合类型数组`a[] | b[]` 不是 `reduce`able,而是 `map`able - Typescript union type array `a[] | b[]` is not `reduce`able, but `map`able 打字稿类型 a 或类型 b - Typescript type a or type b
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM