Skip to content

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

Closed

Description

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])

Activity

  1. IllusionMH commented on Sep 28, 2021

    @IllusionMH
    Contributor

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

  2. a-tarasyuk commented on Sep 28, 2021

    @a-tarasyuk
    Contributor

    I think this PR #45974 handles this case. Playground

  3. aghArdeshir commented on Sep 28, 2021

    @aghArdeshir
    Author

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

  4. aghArdeshir commented on Sep 28, 2021

    @aghArdeshir
    Author

    Yes Andrii Dieiev (@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.

  5. IllusionMH commented on Sep 28, 2021

    @IllusionMH
    Contributor

    Oleksandr Tarasiuk (@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.

  6. typescript-bot commented on Oct 1, 2021

    @typescript-bot
    Contributor

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

  7. locked as resolved and limited conversation to collaborators on Oct 21, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

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