Skip to content

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

Closed
@Jessidhia

Description

@Jessidhia

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)

Metadata

Metadata

Assignees

No one assigned

    Labels

    BugA bug in TypeScriptHas ReproThis issue has compiler-backed repros: https://aka.ms/ts-repros

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions