Microsoft/TypeScript

Error when destructuring deep nested object with literal initializers as fallback

Open

#54 237 ouverte le 13 mai 2023

Voir sur GitHub
 (2 commentaires) (0 réactions) (0 assignés)TypeScript (6 726 forks)batch import
BugDomain: check: Type InferenceHelp Wanted

Métriques du dépôt

Stars
 (48 455 stars)
Métriques de merge PR
 (Merge moyen 6j 17h) (9 PRs mergées en 30 j)

Description

Bug Report

🔎 Search Terms

  • destructuring
  • nested
  • literal initializer

🕗 Version & Regression Information

⏯ Playground Link

Playground link with relevant code

💻 Code

interface Props {
  innerObject?: {
    innerObject2?: {
      name?: string;
      email?: string;
    }
  };
}

// won't let name and email default to undefined
// Initializer provides no value for this binding element and the binding element has no default value. 
export const nestedDestructure1 = (props: Props) => {
  const {
    innerObject: {
       innerObject2: { name, email } = {}
    } = {}
  } = props;
  console.log(name, email);
};

🙁 Actual behavior

Initializer provides no value for this binding element and the binding element has no default value.(2525)

🙂 Expected behavior

name and email default to undefined and has type string | undefined

Guide contributeur