Skip to content

type dosen't infered correctly in loop when strictNullChecks turns on #30308

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
disjukr opened this issue Mar 10, 2019 · 5 comments
Closed

type dosen't infered correctly in loop when strictNullChecks turns on #30308

disjukr opened this issue Mar 10, 2019 · 5 comments
Labels
Design Limitation Constraints of the existing architecture prevent this from being fixed

Comments

@disjukr
Copy link

disjukr commented Mar 10, 2019

TypeScript Version: 3.4.0-dev.20190310

Search Terms: loop, for, while, strictNullChecks, implicitly has type 'any' because it does not have a type annotation and is referenced directly or indirectly in its own initializer.

Code

class A { constructor(a: string) { } }
let a: A | null = null;
while (true) {
    const why = a ? 'a' : 'b';
    a = new A(why);
}

Expected behavior: infer why as string

Actual behavior: why inferred as any

스크린샷 2019-03-10 오후 10 48 11
www typescriptlang org_play_

Playground Link: https://www.typescriptlang.org/play/#src=class%20A%20%7B%20constructor(a%3A%20string)%20%7B%20%7D%20%7D%0D%0Alet%20a%3A%20A%20%7C%20null%20%3D%20null%3B%0D%0Awhile%20(true)%20%7B%0D%0A%20%20%20%20const%20why%20%3D%20a%20%3F%20'a'%20%3A%20'b'%3B%0D%0A%20%20%20%20a%20%3D%20new%20A(why)%3B%0D%0A%7D%0D%0A

Related Issues:

@RyanCavanaugh RyanCavanaugh added the Design Limitation Constraints of the existing architecture prevent this from being fixed label Mar 19, 2019
@RyanCavanaugh
Copy link
Member

The error message means what it says: why does witness itself in its own initializer (in the second iteration of the loop).

@saschanaz
Copy link
Contributor

saschanaz commented Mar 20, 2019

@RyanCavanaugh Can you clarify more? why should be string (or 'a' | 'b'?) in this case even if it does witness itself, because both branches give string anyway.

@saschanaz
Copy link
Contributor

This is still broken only with strictNullChecks 🤷‍♀️

@RyanCavanaugh
Copy link
Member

@saschanaz SNC matters here because otherwise a isn't a union

@saschanaz
Copy link
Contributor

@RyanCavanaugh Yeah, but how does that make a potentially not a string?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Design Limitation Constraints of the existing architecture prevent this from being fixed
Projects
None yet
Development

No branches or pull requests

3 participants