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

Unexpected "Object is possibly undefined" error #53721

Closed
DashwoodIce9 opened this issue Apr 10, 2023 · 3 comments
Closed

Unexpected "Object is possibly undefined" error #53721

DashwoodIce9 opened this issue Apr 10, 2023 · 3 comments
Labels
Duplicate An existing issue was already created

Comments

@DashwoodIce9
Copy link

Bug Report

Hi, I stumbled upon this weird behavior that I couldn't wrap my head around. I searched with "object is possibly undefined bug typescript github" on Google and went through the first 5-6 bugs but didn't find any which quite resembled this case. I am new to Typescript so it is entirely possible that this is expected behavior. If that's the case I apologize and humbly ask for an explanation. Thanks!

🔎 Search Terms

"Object is possibly undefined"

🕗 Version & Regression Information

⏯ Playground Link

Playground link with relevant code

💻 Code

interface TestInterface {
    optionalParam?: {
        [key: string]: string;
    };
}

const result: TestInterface = {
    optionalParam: {
        Key1: "Value1",
    },
};

result.optionalParam["Key2"] = "Value2"; // 'result.optionalParam' is possibly 'undefined'.

🙁 Actual behavior

'result.optionalParam' is possibly 'undefined' error on the last line.

🙂 Expected behavior

Code compiles without any errors. optionalParam is typed as though it can be undefined however, in the example above result.optionalParam is clearly defined. There is no code in between result initialization and result.optionalParam which could set it to undefined. So, this should've worked I think.

@MartinJohns
Copy link
Contributor

Duplicate of #50357.

@RyanCavanaugh RyanCavanaugh added the Duplicate An existing issue was already created label Apr 10, 2023
@DashwoodIce9
Copy link
Author

Hi Martin, thanks for pointing me to the duplicate. I've read through that and #47920 several times over the past 2 days but I couldn't make sense of it. The discussion at #47920 is unfortunately a bit too complicated for me. Could you please let me know (or point me to) if this functionality should be implemented some other way and/or is this not supported by Typescript yet?

The comment at #50357 (comment) says to use -

const anObject: Required<Pick<typeof anotherVar, LongListOfKeys>> = {
  // object initializer with keys from LongListOfKeys
}

But, what are Required and Pick? I didn't get that.

@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

No branches or pull requests

4 participants