Microsoft/TypeScript

Error when destructuring deep nested object with literal initializers as fallback

Open

#54,237 创建于 2023年5月13日

在 GitHub 查看
 (2 评论) (0 反应) (0 负责人)TypeScript (6,726 fork)batch import
BugDomain: check: Type InferenceHelp Wanted

仓库指标

Star
 (48,455 star)
PR 合并指标
 (平均合并 6天 17小时) (30 天内合并 9 个 PR)

描述

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

贡献者指南