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

Destructuring with computed key does not do type inferring even for indexed types #16789

Closed
Jessidhia opened this issue Jun 28, 2017 · 2 comments
Labels
Bug A bug in TypeScript Has Repro This issue has compiler-backed repros: https://aka.ms/ts-repros
Milestone

Comments

@Jessidhia
Copy link

TypeScript Version: 2.4.0

Code

declare const textMap: {[key: string]: string}

function getText (input: string) {
  const {
      [input]: rawText = input
  } = textMap
  // rawText inferred as 'any' and not 'string'
}

Expected behavior:
rawText to be inferred as string

Actual behavior:
rawText inferred as any


As this is a destructuring pattern, it is actually not possible at all to declare the type of rawText other than by making rawText a let declaration first and running an destructuring expression, or otherwise writing equivalent code that does not use destructuring.

let rawText: string
({[input]: rawText = input} = textMap)
@RyanCavanaugh RyanCavanaugh added the Bug A bug in TypeScript label Jun 28, 2017
@mhegazy mhegazy added this to the TypeScript 2.7 milestone Nov 20, 2017
@mhegazy mhegazy modified the milestones: TypeScript 2.7, TypeScript 2.8 Jan 9, 2018
@mhegazy mhegazy modified the milestones: TypeScript 2.8, TypeScript 2.9 Mar 9, 2018
@mhegazy mhegazy modified the milestones: TypeScript 3.0, Future Jul 2, 2018
@sandersn sandersn removed their assignment Jan 7, 2020
@rubiesonthesky
Copy link

It seems that rawText is been inferred as string now

declare const textMap: {[key: string]: string}

function getText (input: string) {
  const {
      [input]: rawText = input
//             ^?
  } = textMap
}

Workbench Repro

@typescript-bot typescript-bot added the Has Repro This issue has compiler-backed repros: https://aka.ms/ts-repros label Mar 10, 2024
@typescript-bot
Copy link
Collaborator

👋 Hi, I'm the Repro bot. I can help narrow down and track compiler bugs across releases! This comment reflects the current state of this repro running against the nightly TypeScript.


Comment by @rubiesonthesky

⚠️ Assertions:

  • const rawText: string

Historical Information
Version Reproduction Outputs
5.0.2, 5.1.3, 5.2.2, 5.3.2, 5.4.2

⚠️ Assertions:

  • const rawText: string

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug A bug in TypeScript Has Repro This issue has compiler-backed repros: https://aka.ms/ts-repros
Projects
None yet
Development

No branches or pull requests

6 participants