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

Optional key with initializer still declared as possibly undefined #50357

Closed
kazarmy opened this issue Aug 18, 2022 · 2 comments
Closed

Optional key with initializer still declared as possibly undefined #50357

kazarmy opened this issue Aug 18, 2022 · 2 comments
Labels
Duplicate An existing issue was already created

Comments

@kazarmy
Copy link

kazarmy commented Aug 18, 2022

Bug Report

πŸ”Ž Search Terms

"Object is possibly" undefined object key

(Sorry couldn't check all relevant issues)

πŸ•— Version & Regression Information

4.7.3

  • I was unable to test this on prior versions because it occurred on new code.

⏯ Playground Link

Playground link (Nightly version) with relevant code

πŸ’» Code

  const anObject: {abc?: string} = {
    abc: '123',
  };
  console.log(anObject.abc.length);

πŸ™ Actual behavior

object-is-possibly-undefined

anObject.abc clearly is not undefined.

πŸ™‚ Expected behavior

There is no error. The actual code has basically typeof anotherVar for the type, and anotherVar's type has a considerable number of keys so it's not just a matter of removing a question mark.

@kazarmy kazarmy changed the title Optional key with initializer still declared undefined Optional key with initializer still declared as possibly undefined Aug 18, 2022
@RyanCavanaugh RyanCavanaugh added the Duplicate An existing issue was already created label Aug 18, 2022
@RyanCavanaugh
Copy link
Member

See discussion at #47920

@kazarmy
Copy link
Author

kazarmy commented Aug 19, 2022

Thanks for the reply. Ok saw the "Optional Member Conformance" section and can make do with

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

which allows dropping of the assert(anObject.key).

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

2 participants