Skip to content

Object is possibly 'undefined' issue #22767

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
spielcrypto opened this issue Mar 21, 2018 · 3 comments
Closed

Object is possibly 'undefined' issue #22767

spielcrypto opened this issue Mar 21, 2018 · 3 comments
Labels
Duplicate An existing issue was already created

Comments

@spielcrypto
Copy link

spielcrypto commented Mar 21, 2018

I'm doing a extreme check to not get object undefined but typescript throw the error Object is possibly 'undefined' anyway

TypeScript Version: 2.7.2

Search Terms:

Code

if (state && state.people && state.people.length) {
                state.people = state.people.sort((person1: IPerson, person2: IPerson) => {
                    const orderBy = state.orderBy;
                    if ( orderBy
                        && person1
                        && person2
                        && Object.keys(person1).length
                        && Object.keys(person2).length
                        && person1[orderBy]
                        && person2[orderBy]) {
                        if (person1[orderBy] < person2[orderBy]) {
                            return (state.sort) ? -1 : 1;
                        }
                        if (person1[orderBy] > person2[orderBy]) {
                            return (state.sort) ? 1 : -1;
                        }
                    }
                    return 0;
                });
}

Expected behavior:
it shouldn't throw the error Object is possibly 'undefined'
Actual behavior:
It throws the error Object is possibly 'undefined' in these if

  • if (person1[orderBy] > person2[orderBy])
  • if (person1[orderBy] < person2[orderBy])

Playground Link:

Related Issues:

@RyanCavanaugh
Copy link
Member

Duplicate #11483

@RyanCavanaugh RyanCavanaugh added the Duplicate An existing issue was already created label Mar 21, 2018
@spielcrypto
Copy link
Author

spielcrypto commented Mar 21, 2018

Ok I found the solution:
this works

const compare = (person1: IPerson, person2: IPerson) => {
            const a = person1[state.orderBy];
            const b = person2[state.orderBy];

            if ( a && b) {
                if (a < b) {
                    return (state.sort) ? -1 : 1;
                }
                if (a > b) {
                    return (state.sort) ? 1 : -1;
                }
            }

            return 0;
};

@typescript-bot
Copy link
Collaborator

Automatically closing this issue for housekeeping purposes. The issue labels indicate that it is unactionable at the moment or has already been addressed.

@microsoft microsoft locked and limited conversation to collaborators Jul 25, 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

3 participants