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

TS does not automatically understand object with accessor type if not extracted to a variable #46111

Closed
aghArdeshir opened this issue Sep 28, 2021 · 6 comments Β· Fixed by #45974
Closed
Labels
Duplicate An existing issue was already created

Comments

@aghArdeshir
Copy link
Contributor

Bug Report

πŸ”Ž Search Terms

extract to variable accessor, accessor automatically infer

πŸ•— Version & Regression Information

  • This is the behavior in every version I tried, and I searched the FAQ page for ].

⏯ Playground Link

Playground link with relevant code

πŸ’» Code

type T_Object = {
    theKey: string;
};

type T_MyType = T_Object | string;

function isItObject(
    input: T_MyType
): input is T_Object {
    return !!(input as T_Object).theKey;
}

const myVariable: { [key: string]: T_MyType } =
    //@ts-ignore
    somehowGetSomething();

function myFunctionWithError
    (key: string) {
    if (isItObject(myVariable[key])) {
        console.log('it is object');
        return myVariable[key].theKey;
    }

    return myVariable[key];
}

function myValidFunction
    (key: string) {
    const extractedVariable = myVariable[key];
    if (isItObject(extractedVariable)) {
        console.log('it is object');
        return extractedVariable.theKey;
    }

    return myVariable[key];
}

πŸ™ Actual behavior

There are two functions at the bottom of the code. The only difference is that in second case, I extracted myVariable[key] to a variable and used that in if condition and return value. In first function it gives me error, but in the second one (extracted variable) it doesn't.

πŸ™‚ Expected behavior

I expected TypeScript to automatically infer myVariable[key] is an object inside the if condition with isItObject(myVariable[key])

@IllusionMH
Copy link
Contributor

IllusionMH commented Sep 28, 2021

Looks like duplicate of #44899 which considered duplicate of #10530 (that accumulated a lot of different cases in comments e.g. #10530 (comment))

@a-tarasyuk
Copy link
Contributor

a-tarasyuk commented Sep 28, 2021

I think this PR #45974 handles this case. Playground

@aghArdeshir
Copy link
Contributor Author

Yes @a-tarasyuk . That would cover this case. Nice πŸ‘

@aghArdeshir
Copy link
Contributor Author

Yes @IllusionMH those two iseues seem like to be a better way of expressing what I wanted. Thanks. πŸ‘

I think this issue can/should be closed.

@IllusionMH
Copy link
Contributor

@a-tarasyuk great work as always!

However is it safe to do this case if myVariable[key] might not return same value between calls?
With intermediate constant you can be sure that constant isn't changed between if and usage but not with property access.

@andrewbranch andrewbranch added the Duplicate An existing issue was already created label Sep 29, 2021
@typescript-bot
Copy link
Collaborator

This issue has been marked as a 'Duplicate' and has seen no recent activity. It has been automatically closed for house-keeping purposes.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Duplicate An existing issue was already created
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants