我玩过routing-controllers
它,它是内置的类转换器能力。我试图构建一个界面,我可以在其中执行基于 alocation id
或location 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'
});
有没有办法做到这一点?