简体   繁体   English

打字稿编译器类型分配错误被忽略

[英]Typescript compiler type assignment error ignored

I have the following scenario: 我有以下情况:

const customer = new Customer();
let customerViewModel = new CustomerLayoutViewModel();

customerViewModel = customer;

This does not produce an error when compiled which in my mind is the wrong behaviour. 编译时不会产生错误,在我看来这是错误的行为。 This seems to be due to the fact that Customer and CustomerLayoutViewModel are completely identical. 这似乎是由于CustomerCustomerLayoutViewModel完全相同。

The problem is that over time they will not be identical and I want the above code to give a compile error because the types are different. 问题是随着时间的推移它们将不完全相同,并且我希望上面的代码给出编译错误,因为类型不同。

So my question: How do I configure the compiler to produce an error in the above example? 所以我的问题是:在上述示例中,如何配置编译器以产生错误?

This will not give compile error because you have not assigned the type of customer or customerViewModel , but if you do something like this then you should get compile time error: 这不会产生编译错误,因为您尚未分配customercustomerViewModel的类型,但是如果执行类似的操作,则应该得到编译时错误:

const customer:Customer = new Customer();
let customerViewModel:CustomerLayoutViewModel = new CustomerLayoutViewModel();

customerViewModel  = customer;

Typescript uses structural typing when determining type compatibility. Typescript在确定类型兼容性时使用结构化类型。 This means that if the two types have a compatible structure they will be compatible: 这意味着,如果两种类型具有兼容的结构,则它们将是兼容的:

class Customer { name: string }
class CustomerLayoutViewModel { name: string }
const customer = new Customer();
let customerViewModel = new CustomerLayoutViewModel();
customerViewModel = customer; // OK compatible

If Customer has extra properties the types are still compatible, there is no chance someone will access through customerViewModel something that isn't there: 如果Customer具有额外的属性,这些类型仍然兼容,则没有机会有人通过customerViewModel访问不存在的内容:

class Customer { name: string; fullName: string }
class CustomerLayoutViewModel { name: string }
const customer = new Customer();
let customerViewModel = new CustomerLayoutViewModel();
customerViewModel = customer; // still OK compatible

You will get compatibility errors if CustomerLayoutViewModel has extra required properties: 如果CustomerLayoutViewModel具有额外的必需属性,则会出现兼容性错误:

class Customer { name: string }
class CustomerLayoutViewModel { name: string; fullName: string }
const customer = new Customer();
let customerViewModel = new CustomerLayoutViewModel();
customerViewModel = customer; //error now

The one way to ensure types are incompatible it to add a private field to a class. 确保类型不兼容的一种方法是将私有字段添加到类。 private fields will not be compatible with any other field in any other class event if the name is the same: 如果名称相同,则私有字段将与任何其他类事件中的任何其他字段不兼容:

class Customer { private x: string }
class CustomerLayoutViewModel { private x: string }
const customer = new Customer();
let customerViewModel = new CustomerLayoutViewModel();
customerViewModel = customer; //error Types have separate declarations of a private property 'x'. 

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

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