简体   繁体   English

TypeScript 通过 object 解构允许不正确的属性

[英]TypeScript allowing incorrect properties via object destructuring

I'm running into an issue with TypeScript whereby an object using one type can be injected via a destructing assignment into an another object that uses a completely different type, and TypeScript doesn't show any errors for it. I'm running into an issue with TypeScript whereby an object using one type can be injected via a destructing assignment into an another object that uses a completely different type, and TypeScript doesn't show any errors for it.

An example being:一个例子是:

interface Pizza {
    cookingTime: number;
    size: "Small" | "Medium" | "Large";
}

interface Car {
    modelName: string;
    yearManufactured: number;
}

const produceMalformedPizza = (car: Car): Pizza => {
    return {
        ...car, // This is not a "Pizza" object but no errors are raised
        cookingTime: 10,
        size: "Small",
    }
}

console.log(
    produceMalformedPizza({
        modelName: "NotAPizza",
        yearManufactured: 1980
    })
)

/* Output:
[LOG]: {
  "modelName": "NotAPizza",
  "yearManufactured": 1980,
  "cookingTime": 10,
  "size": "Small"
} 
*/

I've tried to turn every possible strict flag on — and you can see it compiling online here .我已经尝试打开所有可能的严格标志——你可以在这里看到它在线编译。

If I try to return any specific properties in the produceMalformedPizza function, then the compiler raises errors, as expected — but it seems as though I can inject arbitrary object data using a destructuring (spread) assignment.如果我尝试在produceMalformedPizza function 中返回任何特定属性,那么编译器会如预期的那样引发错误——但似乎我可以使用解构(扩展)分配注入任意object 数据。

I'm relatively new to TypeScript, so perhaps this is expected behaviour?我对 TypeScript 比较陌生,所以也许这是预期的行为? Ie I can't use object spread destructuring with inferred type safety?即我不能使用 object 传播解构与推断类型安全?

Edit: As per the answer from @@TJCrowder this is intentional.编辑:根据@@TJCrowder 的回答,这是故意的。 There is some GitHub discussion about this issue here: https://github.com/microsoft/TypeScript/issues/43499 , and also a proposal to support "Exact" types here: https://github.com/microsoft/TypeScript/issues/12936这里有一些关于这个问题的 GitHub 讨论: https://github.com/microsoft/TypeScript/issues/43499 ,还有一个支持“精确”类型的提议: Z5E056C500A1C4B6A71Z10B50Dmicrosoft8/07A71Z10B50Dmicrosoft问题/12936

It's expected behavior.这是预期的行为。 The object you're returning is a subtype of Pizza — it has all the properties Pizza requires (plus some that it doesn't).您返回的 object 是Pizza子类型——它具有Pizza所需的所有属性(加上一些它不需要的属性)。

TypeScript does do "excess property checks" but only in limited situations. TypeScript 确实会进行“额外的属性检查”,但仅限于有限的情况。 For instance, if you were to put a non- Pizza property explicitly in that object literal, TypeScript would flag that up not because it's actually wrong from a type perspective (it's fine to use a subtype) but because it's probably a programmer mistake.例如,如果您要在 object 文字中显式放置非Pizza属性,则 TypeScript 会标记它,不是因为从类型角度来看它实际上是错误的(使用子类型很好),而是因为它可能是程序员的错误。

I'm slightly surprised it doesn't do that with ...car (since car has required properties), but it doesn't do excess property checks in all situations.我有点惊讶它对...car没有这样做(因为car具有必需的属性),但它不会在所有情况下都进行过多的属性检查。

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

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