2

Consider the following code:

let str: string | null;

function print(msg: string) {
    console.log(msg);
}


print(str);

in this case typescript compiler give me an error, saying correctly that Argument of type 'string | null' is not assignable to parameter of type 'string'.

This could simply fixed checking for str existence, so

let str: string | null;

function print(msg: string) {
    console.log(msg);
}

if (str) {
    print(str);
}

compile without errors. Typescript compiler i smart enough to understand the check.

Now suppose you check variable existence within a method, for example

let str: string | null;

function print(msg: string) {
    console.log(msg);
}

function check(str: string) {
    return str != null;
}

if (check(str)) {
    print(str);
}

In this case typescrip do not understand that the call to print method is safe. How can I fix this?


EDIT

To be clear, this is (more or less) the skelethon of my class:

ok, but my case is a little bit more complex. This is, more or less the structure of my class:

class Clazz {
    private myProp: {
        aString?: string
        anotherString?: number
    };

    constructor(aParam: any) {
        this.myProp = {};
        if (aParam.aString) {
            this.myProp.aString = aParam.aString;
        }

        if (aParam.anotherString) {
            this.myProp.anotherString = aParam.anotherString;
        }
    }

    public haveAString() {
        return this.myProp.aString != null;
    }

    public haveAnotherString() {
        return this.myProp.anotherString != null;
    }

    public computation1() {
        if (this.haveAString()) {
            this.doAComputation(this.myProp.aString);
        }
    }

    public computation2() {
        if (this.haveAnotherString()) {
            this.doAComputation(this.myProp.anotherString);
        }
    }

    private doAComputation(str: string) {
        // do something with the string
    }

}

How should I fix in my case?

1 Answer 1

2

The compiler will not follow checks across function boundaries, but you can use a custom type guard to achieve the same effect

let str: string | null;

function print(msg: string) {
    console.log(msg);
}

function check(str: string| null) : str is string{
    return str != null;
}

if (check(str)) {
    print(str);
}
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks! Now, my case is a little different, my check method do not accept params. I've edited my question..
@Canemacchina type guards and private members don't play well.. Maybe we could get something to work for a public member but for privates I don't think there is any hope.. Still thinking about it though, maybe someone else has an idea...
I'll end up with good tests and manually instructing the compiler that aString is not null or undef

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.