0

我玩过routing-controllers它,它是内置的类转换器能力。我试图构建一个界面,我可以在其中执行基于 alocation idlocation coordinate. 所以我打算使用一个有区别的联合作为一个身体参数并且不能让它工作。(请参阅最后一个控制台输出,我所说的“不工作”是什么意思)

举个例子:

interface LocationCoordinates {
    type: 'coordinate'
    longitude: number
    latitude: number
}

interface LocationId {
    type: 'id'
    id: number
}

class LocationRadius {
    data: LocationCoordinates | LocationId
    searchRadiusInKm: number
}

// raw input for LocationCoordinates
const rawLocationCoordinates = {
    data: {
        longitude: 22,
        latitude: 33
    },
    searchRadiusInKm: 30
}


// raw input for LocationId
const rawLocationId = {
    data: {
        id: 1
    },
    searchRadiusInKm: 30
}
// transfrom both raw inputs
const realLocationCoordinates = plainToClass(LocationRadius, rawLocationCoordinates);
const realLocationId = plainToClass(LocationRadius, rawLocationId);


console.log({
    coordinateType: realLocationCoordinates.data.type, // expect 'coordinate' but got 'undefinded'
    idType: realLocationId.data.type // expect 'id' but got 'undefinded'
});

有没有办法做到这一点?

4

1 回答 1

1

您可以这样做,但您需要进行一些更改:

  1. LocationIdLocationCoordinates应该是类
  2. 您应该@Type向输入属性添加一个装饰器。这允许class-transformer根据特定的鉴别器参数处理反序列化
class LocationRadius {
 @Type(() => Object, {
     keepDiscriminatorProperty: true,
     discriminator: {
         property: "type",
         subTypes: [
             { value: LocationCoordinates, name: "coordinate" },
             { value: LocationId, name: "id" }
         ]
     }
 })
 data: LocationCoordinates | LocationId
 searchRadiusInKm: number
}
  1. 您应该在输入中添加一个type 属性,以便甚至 TS 也可以区分联合:
 // raw input for LocationCoordinates
const rawLocationCoordinates = {
   data: {
       type: "coordinate",
       longitude: 22,
       latitude: 33
   },
   searchRadiusInKm: 30
}

你可以在我设置的这个StackBlitz 项目中看到结果

于 2020-04-26T16:56:17.910 回答