Skip to content

Weird 'referenced directly or indirectly in its own initializer' error #14428

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

Closed
vilicvane opened this issue Mar 2, 2017 · 2 comments
Closed
Assignees
Labels
Bug A bug in TypeScript Fixed A PR has been merged for this issue

Comments

@vilicvane
Copy link

TypeScript Version: 2.2.1

Code

(async () => {
    function foo(p: string[]): string[] {
        return [];
    }

    function bar(p: string[]): string[] {
        return [];
    }

    let a1: string[] | undefined = [];

    while (true) {
        let a2 = foo(a1!);
        a1 = await bar(a2);
    }
});

With noImplicitAny and strictNullChecks turned on, this gives error:

'a2' implicitly has type 'any' because it does not have a type annotation and is referenced directly or indirectly in its own initializer.
@ahejlsberg ahejlsberg self-assigned this Mar 3, 2017
@ahejlsberg ahejlsberg added the Bug A bug in TypeScript label Mar 3, 2017
@ahejlsberg ahejlsberg added this to the TypeScript 2.3 milestone Mar 3, 2017
@ahejlsberg
Copy link
Member

When inferring the type of a variable from its initializer we're performing a full type check of the initializer instead of just doing the minimal work to obtain its type (i.e. we're using checkExpression instead of getTypeOfExpression). This full type check causes a circularity to surface. We just need to be a little less eager.

@ahejlsberg ahejlsberg added the Fixed A PR has been merged for this issue label Mar 4, 2017
@ghost
Copy link

ghost commented May 31, 2017

A possibly related case:

type AB = A | B;
class A { _a: any; }
class B { _b: any; }

declare function create<T>(b: boolean): T;

function f() {
    let ab: AB = new B();
    while (true) {
        const b = create<B>(ab !== undefined);
        ab = b;
    }
}

src/a.ts(10,15): error TS7022: 'b' implicitly has type 'any' because it does not have a type annotation and is referenced directly or indirectly in its own initializer.

@microsoft microsoft locked and limited conversation to collaborators Jun 19, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Bug A bug in TypeScript Fixed A PR has been merged for this issue
Projects
None yet
Development

No branches or pull requests

2 participants