我是TypeScript的初学者(很明显,我来自Java Universe)。我目前正试图做一个这样的映射器:
public getApercuTypePrestationFromTypePrestationEX044(typePrestationEX044: TypePrestationEX044): ApercuTypePrestation {
let apercuTypePrestation: ApercuTypePrestation;
if (null != typePrestationEX044) {
apercuTypePrestation = new ApercuTypePrestation();
apercuTypePrestation.codeTypePrestation == typePrestationEX044.code;
apercuTypePrestation.libelleTypePrestation == typePrestationEX044.libelle;
}
console.log("A = " + typePrestationEX044.code);
console.log("B = " + apercuTypePrestation.libelleTypePrestation);
return apercuTypePrestation;
}
但是它显然不起作用:在控制台中,我有: A = A8C B =未定义
我该如何解决?
答案 0 :(得分:1)
您使用的是==
而不是=
。
我将==
更改为=
,现在应该可以使用了。
public getApercuTypePrestationFromTypePrestationEX044(typePrestationEX044: TypePrestationEX044): ApercuTypePrestation {
let apercuTypePrestation: ApercuTypePrestation;
if (null != typePrestationEX044) {
apercuTypePrestation = new ApercuTypePrestation();
apercuTypePrestation.codeTypePrestation = typePrestationEX044.code;
apercuTypePrestation.libelleTypePrestation = typePrestationEX044.libelle;
}
console.log("A = " + typePrestationEX044.code);
console.log("B = " + apercuTypePrestation.libelleTypePrestation);
return apercuTypePrestation;
}
在打字稿中,
==
或===
用于比较而不是分配,以便分配必须使用=
更新
我还注意到您正在错误地检查typePrestationEX044
中是否为null。
更改此:
if (null != typePrestationEX044) {
apercuTypePrestation = new ApercuTypePrestation();
apercuTypePrestation.codeTypePrestation = typePrestationEX044.code;
apercuTypePrestation.libelleTypePrestation = typePrestationEX044.libelle;
}
为此
if (typePrestationEX044) {
apercuTypePrestation = new ApercuTypePrestation();
apercuTypePrestation.codeTypePrestation = typePrestationEX044.code;
apercuTypePrestation.libelleTypePrestation = typePrestationEX044.libelle;
}
此if
条件将自动检查undefined
,null
或boolean