Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

typescript forEach expecting additional conditions #24213

Closed
dgofman-equinix opened this issue May 17, 2018 · 4 comments
Closed

typescript forEach expecting additional conditions #24213

dgofman-equinix opened this issue May 17, 2018 · 4 comments
Labels
Duplicate An existing issue was already created

Comments

@dgofman-equinix
Copy link

dgofman-equinix commented May 17, 2018

I cannot understand why in typescript forEach loop the behavior is different comparing with for, for in, or for of loops???


let a: {str: string} | null = null,
    array = ["WORLD", "!"];
if (!a) {
    a = {str: "HELLO"};
    array.forEach(x => {
        a.str += x; //WHY???
        if (a) {
            a.str += x; //OK
        }
        if (a.str) { //HOW???
            a.str += x; //error
        }
        if (a && a.str) { //OK
            a.str += x; //OK
        }
    });

    for (let i = 0; i < array.length; i++) {
        a.str += array[i]; //OK
    }

    for (let i in array) {
        a.str += array[i]; //OK
    }

    for (let i of array) {
        a.str += i; //OK
    }
}
@mhegazy
Copy link
Contributor

mhegazy commented May 17, 2018

Please see #10982 (comment) for an explanation.

consider using const a = { str: "HELLO" }; instead.

@mhegazy mhegazy added the Duplicate An existing issue was already created label May 17, 2018
@dgofman-equinix
Copy link
Author

But my variable is dynamic based on if condition or may be initializing from constructor. I cannot use const the best what can I do use "readonly" (finalized) . But I will get the same issue.

@dgofman-equinix
Copy link
Author

dgofman-equinix commented May 18, 2018

Why in my static function I have same issue. I am validating "a" (is not null). Other loops and "return" from a function are not complaining except forEach function.

static concat(a:{str: string} | null, array = ["WORLD", "!"]): string {
        if (!a) {
            a = {str: "HELLO"};
            array.forEach(x => {
                a.str += x; //WHY???
                if (a) {
                    a.str += x; //OK
                }
                if (a.str) { //HOW???
                    a.str += x; //error
                }
                if (a && a.str) { //OK
                    a.str += x; //OK
                }
            });

            for (let i = 0; i < array.length; i++) {
                a.str += array[i]; //OK
            }

            for (let i in array) {
                a.str += array[i]; //OK
            }

            for (let i of array) {
                a.str += i; //OK
            }
        }
        return a.str; //OK
    }

@typescript-bot
Copy link
Collaborator

Automatically closing this issue for housekeeping purposes. The issue labels indicate that it is unactionable at the moment or has already been addressed.

@microsoft microsoft locked and limited conversation to collaborators Aug 2, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Duplicate An existing issue was already created
Projects
None yet
Development

No branches or pull requests

3 participants