Skip to content

Convert to optional chain expression action creates incorrect code #62553

@rubiesonthesky

Description

@rubiesonthesky

🔎 Search Terms

Convert to optional chain

🕗 Version & Regression Information

  • This changed between versions ______ and ______
  • This changed in commit or PR _______
  • This is the behavior in every version I tried, and I reviewed the FAQ for entries about _________
  • I was unable to test this on prior versions because playground doesn't support code actions

I'm able to repro this with versions 5.8.3 and 5.9.2 and VS Code Version: 1.104.3

⏯ Playground Link

https://www.typescriptlang.org/play/?#code/JYOwLgpgTgZghgYwgAgCJzHZBvAUM5AawgE91MB+ALmQGlTy4BuXAX111ElkRXrIxY8BYiRoBnMFFABzFgQAmEcQmkAHMMAD2IasknSQcthyUIANnCgoEOycgWCajFrZD3RjAMpTZyALwOggB0noLIFEGYoQwhosgA1MgA5FTJicgAFI7RYdFKKuqaOhGRyckAlMg05UwE9Q2NTc0tLbgA9O2t3T29jQB6FBydyABKygCu5mDIWjDIYAAWKIjFIDQdXW4esZg+hjIBUXAxAnu+RhSnpSnJTEA

💻 Code

interface Data {
  keyData?: KeyData;
}

interface KeyData {
  key: string;
  description?: string;
}

declare const data: Data;
const keyDataString = data.keyData ? data.keyData.key + ':' + (data.keyData.description ?? '') : '';

Try to invoke "Convert to optional chain expression" code action before colon.

🙁 Actual behavior

This is result of the action

const keyDataString = data.keyData?.key ?? '';

🙂 Expected behavior

It should not offer the action here as it's not a simple case.

Additional information about the issue

I was able to repro this with the tsconfig settings used in Playground but also in VS Code editor without tsconfig file.

Metadata

Metadata

Assignees

No one assigned

    Labels

    BugA bug in TypeScriptDomain: Refactoringse.g. extract to constant or function, rename symbol

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions