Skip to content

Do...while types not treated same as while types #3810

@muglug

Description

@muglug

I think there's an issue dealing with the do statement here:

const getSomeParentNode = (element: Element) => {
    let elementParent = element.parentNode;

    if (!elementParent) {
        return null;
    }

    do {
        elementParent = elementParent.parentNode;

        if (!elementParent) {
            return null;
        }
    } while (elementParent !== document.body);

    return elementParent;
};
11: elementParent = elementParent.parentNode;
                                    ^ property `parentNode`. Property cannot be accessed on possibly null value

Whereas the regular while loop version correctly recalculates the type of elementParent at the top of the loop.

const getSomeParentNode = (element: Element) => {
    let elementParent = element.parentNode;

    if (!elementParent) {
        return null;
    }

    while (elementParent !== document.body) {
        elementParent = elementParent.parentNode;

        if (!elementParent) {
            return null;
        }
    } 

    return elementParent;
};

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions