Skip to content
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

Wrong type definition #9324

Closed
cawa-93 opened this issue Jun 23, 2016 · 2 comments
Closed

Wrong type definition #9324

cawa-93 opened this issue Jun 23, 2016 · 2 comments

Comments

@cawa-93
Copy link

cawa-93 commented Jun 23, 2016

TypeScript Version:
1.9.0
Atom-TypeScript plugin Version:
8.11.0
Atom Version:
1.7.4
Code method of Class

setProfiles(newProfiles: IProfiles[]) {
    let needSearch = true;
        // By default, the variable is of type
        // this.stg.profiles: IProfiles[] | undefined
    if (!this.stg.profiles) {
        this.stg.profiles = [];
        needSearch = false;
    }
    // On the line below the variable type is defined as
    // this.stg.profiles: IProfiles[]
    if (!angular.isArray(newProfiles)) return this.stg.profiles;

    newProfiles.map((profile) => {
        if (!profile || !profile.id) return;
        const index = needSearch ? this.getProfileIndex(profile.id) : -1;

        if (index >= 0) {
            // However, on the line below the variable type is defined as
            // this.stg.profiles: IProfiles[] | undefined
            this.stg.profiles[index] = profile;
        } else {
            // And in the line below too
            // this.stg.profiles: IProfiles[] | undefined
            this.stg.profiles.unshift(profile);
        }
    });

    // Agin, On the line below the variable type is defined as
    // this.stg.profiles: IProfiles[]
    return this.stg.profiles;
}

As you can see from the comments, the type of a variable is not defined clearly in the callback function.
Why is this happening? How can I fix it?

@kitsonk
Copy link
Contributor

kitsonk commented Jun 23, 2016

Dupe of #7662 as narrowing is reset across function boundaries as explained in #7718 (comment).

@cawa-93
Copy link
Author

cawa-93 commented Jun 23, 2016

@kitsonk, thanks for the answer

@cawa-93 cawa-93 closed this as completed Jun 23, 2016
@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
None yet
Projects
None yet
Development

No branches or pull requests

2 participants