简体   繁体   English

将对象类型转换为另一种类型

[英]Cast object type to another type

I have class where I set type of range to IntervalRange 我有一个将range类型设置为IntervalRange

export class Test {range: IntervalRange;}

then in parent class I initialize the value: 然后在父类中,我初始化值:

export class TestInitializer {
Create(){
    return <Test>{
        range: IntervalRange.initialize(Database.GetByName('MONTH').id};
}

InitializeElement() {
  let test = <Test>JSON.parse(JSON.stringify(configuration));
  return test;
}

then in my other component I use this as: 然后在其他组件中,我将其用作:

@Input() range: IntervalRange;

However on function range.getRange(); 但是在函数range.getRange(); I get: ERROR TypeError: this.range.getRange is not a function 我得到: ERROR TypeError: this.range.getRange is not a function

Why is that? 这是为什么? it says range is an Object, though it should be IntervalRange . 它说range是一个对象,尽管应该是IntervalRange

I tried writing as IntervalRange , <IntervalRange> range nothing worked. 我尝试编写as IntervalRange<IntervalRange> range无效。 How to fix that? 如何解决?

Update: let type = typeof(this.range) ; 更新: let type = typeof(this.range) ; prints "object" 打印"object"

method: 方法:

ngOnChanges() {
if (this.range) {
  let type = typeof(this.range);
  let ddd = this.range.getRange(); //<----- this is where I get error
}

A typecast only casts the type. 类型转换仅强制转换类型。 Typescript doesn't exist at runtime. Typescript在运行时不存在。 Therefore if JSON.parse doesn't return a proper Test instance (which it won't since methods won't get serialized), it will fail at runtime. 因此,如果JSON.parse没有返回正确的Test实例(由于方法不会序列化,则不会返回),它将在运行时失败。 Instead of typecasting you probably want to build up a Test instance, and load the serialized data into it. 您可能不希望进行类型转换,而是要构建一个Test实例,然后将序列化的数据加载到其中。

The TypeScript "cast" ( <A>tmp or tmp as A ) : TypeScript“广播”( <A>tmptmp as A ):

  • makes structural type compatibility checking → it is unavailable if the source data has type any . 进行结构类型兼容性检查→如果源数据的类型为any则不可用。
  • has no runtime counterpart → TypeScript types can be compatible but runtime types can differ, for instance an object literal vs a real class instance. 没有运行时对应项→TypeScript类型可以兼容,但运行时类型可以不同,例如,对象文字与真实类实例。

When the data comes from an unsafe/external source, eg JSON.parse() or a WebAPI call, it's a DTO (data transfer object) , with the any type. 当数据来自不安全/外部来源(例如JSON.parse()或WebAPI调用)时,它是DTO (数据传输对象) ,具有any类型。 The TypeScript "cast" is unsafe too. TypeScript“ cast”也是不安全的。

To ensure the cast operation, you can use a mapping function from the DTO to the "domain model" class. 为了确保强制转换操作,可以使用从DTO到“域模型”类的映射功能。 The key point is to return a real class instance. 关键是返回真实的类实例。

  • Object.assign(new IntervalRange(), this.range) (that you mentioned in a comment) satisfies this point. Object.assign(new IntervalRange(), this.range) (您在注释中提到的)满足了这一点。
  • We can be even more stricter with a "field by field mapping": this.a = range.a; this.b = range.b; ...) 使用“逐字段映射”可以使我们更加严格: this.a = range.a; this.b = range.b; ...) this.a = range.a; this.b = range.b; ...) this.a = range.a; this.b = range.b; ...) . this.a = range.a; this.b = range.b; ...)

Both options are better encaspulated in a function: the class constructor, a static factory method in the class, an external function in the same module. 这两个选项最好都包含在函数中:类构造函数,类中的静态工厂方法,同一模块中的外部函数。

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

相关问题 如何在 JavaScript 中将普通 object 转换为另一种类型的实例 - How to cast a plain object as a instance of another type in JavaScript “Null”类型不是“List”类型的子类型<map<String,Object> &gt;&#39; 类型转换 - type 'Null' is not a subtype of type 'List<map<String,Object>>' in type cast 是否可以在Flow中将Object强制转换为确切类型? - Is it possible to cast an Object to an exact type in Flow? 将 object 嵌套属性转换为新类型 - Cast object nested properties to new type “为值转换为数字失败”(类型对象)- Mongoose - "Cast to Number failed for value " (type Object) - Mongoose 我们可以在 javascript 中将通用对象转换为自定义对象类型吗? - Can we cast a generic object to a custom object type in javascript? 如何在Javascript中将变量强制转换为另一个变量的类型 - How can I cast a variable to the type of another in Javascript 根据TypeScript中另一个对象的类型创建一个对象 - create an object based on the type of another object in TypeScript 在 JavaScript 中将对象转换为另一种类型的对象 - Convert an object into another type of object in JavaScript Javascript - 将特定的 Object 转换为另一种类型的 object? - Javascript - Convert specific Object to another type of object?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM