Skip to content

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

Closed
@goldenbearkin

Description

@goldenbearkin

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'

Metadata

Metadata

Assignees

No one assigned

    Labels

    DuplicateAn existing issue was already created

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions