Skip to content

Spurious "referenced directly or indirectly in its own initializer" when combined with narrowing, asserts function, and loopΒ #53955

Open
@jakebailey

Description

@jakebailey

Bug Report

πŸ”Ž Search Terms

referenced directly or indirectly asserts

πŸ•— Version & Regression Information

  • This is the behavior in every version I tried

⏯ Playground Link

Playground Link

πŸ’» Code

declare const myRequire: ((p: string) => {} | undefined) | undefined;

declare function assertIsDefined<T>(value: T): asserts value is NonNullable<T>

function fn1() {
    if (!myRequire) {
        return;
    }

    for (const p of ["a", "b"]) {
        // Error?
        const result = myRequire("something");
        assertIsDefined(result);
    }
}

πŸ™ Actual behavior

result is claimed to be referenced directly or indirectly in its initializer.

Modifying the code in various ways makes the error go away.

πŸ™‚ Expected behavior

No error; there's no relationship between the initializer and the result.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions