在块外访问变量

时间:2019-04-10 08:10:48

标签: typescript

我是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 =未定义

我该如何解决?

1 个答案:

答案 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条件将自动检查undefinednullboolean