AutoMapper-TS映射对象是空的,当我尝试将其转换为一种类型,打字稿

问题描述:

我有以下类:AutoMapper-TS映射对象是空的,当我尝试将其转换为一种类型,打字稿

class testClass 
{ 
    public text: string; 
} 

我使用AutoMapper-TS创建如下图:

AutoMapper.createMap('source', 'dest') 
      .convertToType(testClass); 

然后做我的映射:

this.newObject = AutoMapper.map('source', 'dest', this.anotherObjectWithSameProperties); 

this.newObject始终是一个空testClass对象。 如果删除了以下行:

.convertToType(testClass); 

然后按预期的属性将被映射到this.newObject,但this.newObject的类型将是Object代替testClass

如何使用automapper-ts将对象映射到另一个对象,但将新对象作为所需类型?

UPDATE

的AutoMapper-TS documentation显示情况下,这种方法有一个单元测试,但这个单元测试指定明确的映射:

.forMember('property', (opts: AutoMapperJs.IMemberConfigurationOptions) => { opts.mapFrom('ApiProperty'); }) 

如果我这样做,我text财产,像这样:

AutoMapper.createMap('source', 'dest') 
    .forMember('text', 'text'); 
    .convertToType(testClass); 

然后我的实现按预期工作,但临时工t首先反驳了使用automapper的全部观点。

投放至指定类型的地图后,像这样:

this.newObject = AutoMapper.map('source', 'dest', this.anotherObjectWithSameProperties) as testClass; 

this.newObject = <testClass> AutoMapper.map('source', 'dest', this.anotherObjectWithSameProperties); 
+0

都能跟得上。 'this.newObject'仍然是一个Object而不是'testClass'。 – Viqas