Skip to content

Control flow based type analysis not working inside an callback function #10051

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
goldenbearkin opened this issue Jul 31, 2016 · 2 comments
Closed
Labels
Duplicate An existing issue was already created

Comments

@goldenbearkin
Copy link

goldenbearkin commented Jul 31, 2016

TypeScript Version: nightly (2.0.0)

Code
// Below I am returning Observable, but I've tried returning a pure callback function and it doesn't work either.

interface IIdProvider {
    readonly idToken$: Observable<string>;
    readonly providerURL: string;

}
public getLastIdProvider(): IIdProvider | null;

public isAuthenticated(): boolean | Observable<boolean> {
    let idProvider = this.getLastIdProvider();

    if (idProvider === null) return false;

    idProvider.idToken$ 
      .subscribe(x => console.log(x));   // OK

    return Observable.create(observer => {
      idProvider.idToken$ // TS2531: Object is possibly null
        .subscribe(x => console.log(x));
        ....

// for Pure callback function version:
 public isAuthenticated(): boolean | (() => void) {

    let idProvider = this.getLastIdProvider();

    if (idProvider === null) return false;

    idProvider.idToken$ //OK
      .subscribe(x => console.log(x));

    return () => {
      idProvider.idToken$
        .subscribe(x => console.log(x));  // TS2531: Object is possibly null
    };

Expected behavior:
both the idProvider should be null following control flow based type analysis.

Actual behavior:
The second one inside an observable creating function prompts warning of 'possibly null'

@goldenbearkin goldenbearkin changed the title Control flow based type analysis not working inside an returning function Control flow based type analysis not working inside an callback function Jul 31, 2016
@ahejlsberg
Copy link
Member

Duplicate of #7719. If you change your local variable declaration to const idProvider = ... it should work.

@ahejlsberg ahejlsberg added the Duplicate An existing issue was already created label Jul 31, 2016
@goldenbearkin
Copy link
Author

It works. Thanks.

@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
Duplicate An existing issue was already created
Projects
None yet
Development

No branches or pull requests

2 participants