GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — Refactoring to convert && chain to optional chain expression · Issue #35018 · microsoft/TypeScript

Refactoring to convert && chain to optional chain expression #35018

New issue
Copy link
New issue
Copy link
Closed
#39135
Closed
Refactoring to convert && chain to optional chain expression#35018
#39135
Copy link
Assignees
minestarksjessetrinity
Labels
CommittedThe team has roadmapped this issueThe team has roadmapped this issueDomain: LS: Refactoringse.g. extract to constant or function, rename symbole.g. extract to constant or function, rename symbolFix AvailableA PR has been opened for this issueA PR has been opened for this issueSuggestionAn idea for TypeScriptAn idea for TypeScript
Milestone
TypeScript 4.0.1

Description

@DanielRosenwasser
Daniel Rosenwasser (DanielRosenwasser)
opened on Nov 9, 2019
Issue body actions
interface Foo {
    bar?: {
        baz?: string;
    }
}

declare let foo: Foo | undefined;

[|foo && foo.bar && foo.bar.baz|]

It should be possible to convert that chain into the following:

interface Foo {
    bar?: {
        baz?: string;
    }
}

declare let foo: Foo | undefined;

foo?.bar?.baz;
Reacted by Bruce Pascoe, Eden Turgeman, Nico Jansen, Borui Gu, Ismail Ghallou , Mattias Buelens, Jeroen Meijer (Jay), Justin Grant, harrisoncarter, Gabriel Francisco and 16 more
Reacted by AnyhowStep, Michał Gołębiowski-Owczarek, Matt Kane, Matija Grcic, Jiri Spac, Steven, Orta Therox, Andrii Byk, Dustin Savery, Eden Turgeman and 18 more

Activity

  1. Daniel Rosenwasser (DanielRosenwasser)
    added
    SuggestionAn idea for TypeScriptAn idea for TypeScript
    Domain: LS: Refactoringse.g. extract to constant or function, rename symbole.g. extract to constant or function, rename symbol
    on Nov 9, 2019
  2. Daniel Rosenwasser (DanielRosenwasser)
    added this to the Backlog milestone on Nov 9, 2019
  3. Daniel Rosenwasser (DanielRosenwasser)
    self-assigned this
    on Nov 9, 2019
  4. DanielRosenwasser commented on Nov 9, 2019

    @DanielRosenwasser
    Daniel Rosenwasser (DanielRosenwasser)
    on Nov 9, 2019
    MemberAuthor
    More actions

    You know, mostly like this

    convertToOptionalChain

    Reacted by Matija Grcic, Jiri Spac, Steven, Nico Jansen, Alex Quintero, Ismail Ghallou , Bruno Antunes, ExE Boss, Luciano Lima, Abhishek Datta and 6 more
    Reacted by Bruno Antunes, Michael Loughry, Mohit Mayank, Luciano Lima, Abhishek Datta, Roger Padilla, Ali Taheri Moghaddar and AwesomeObserver
  5. Urigo commented on Nov 9, 2019

    @Urigo
    Uri Goldshtein (Urigo)
    on Nov 9, 2019
    More actions

    maybe a code-mode or babel transform that runs throughout a codebase?

  6. DanielRosenwasser commented on Nov 9, 2019

    @DanielRosenwasser
    Daniel Rosenwasser (DanielRosenwasser)
    on Nov 9, 2019
    MemberAuthor
    More actions

    Uri Goldshtein (@Urigo) That could be done too, but I think having this granularity is good because it's not strictly the same semantics. ?. always returns undefined, && keeps the original nullish value.

    Reacted by Ian Copp
  7. switz commented on Nov 10, 2019

    @switz
    Daniel Saewitz (switz)
    on Nov 10, 2019
    More actions

    Also keep in mind that optional chaining returns true on 0 and '' (empty string), normally falsy values in javascript. So running this as a code mod would almost certainly cause unintended bugs.

    Reacted by Nico Jansen, Kristiqn Tachev, Johan Faerch, Justin Grant, Max Schmitt, Carlos Abbate, Ian Copp, Josef and Ali Taheri Moghaddar
  8. MLoughry commented on Nov 11, 2019

    @MLoughry
    Michael Loughry (MLoughry)
    on Nov 11, 2019
    More actions

    Also keep in mind that optional chaining returns true on 0 and '' (empty string), normally falsy values in javascript. So running this as a code mod would almost certainly cause unintended bugs.

    I'm not sure I really follow. Such code would be rather questionable in the first place, no? If you have an && chain that could be converted to an optional chain, and it returns '' or false as a short-circuit, then the same code would be throwing a TypeError if the value is a non-empty string or true.

  9. fatcerberus commented on Nov 12, 2019

    @fatcerberus
    Bruce Pascoe (fatcerberus)
    on Nov 12, 2019
    More actions

    I could certainly envision a scenario where code was written that depends on short-circuiting on falsy values. We're eternally stuck with typeof null === 'object' for backward compatibility reasons, after all.

    Also there's no TypeError because boolean and string are object-coercible. The subsequent property access would just return undefined and short-circuit.

  10. jineshshah36 commented on Nov 17, 2019

    @jineshshah36
    Jinesh Shah (jineshshah36)
    on Nov 17, 2019
    More actions

    [|foo && foo.bar && foo.bar.baz|]

    What do the “|” mean?

  11. dragomirtitian commented on Nov 18, 2019

    @dragomirtitian
    Titian Cernicova-Dragomir (dragomirtitian)
    on Nov 18, 2019
    Contributor
    More actions

    Jinesh Shah (@jineshshah36) [|code|] is the syntax used typescript four-slash tests to mark a selection. Some diagnostic suggestions are not exposed unless they can be applied to the selected code.

    Reacted by Jinesh Shah and ExE Boss
  12. MrAndersen1 commented on Dec 9, 2019

    @MrAndersen1
    MrAndersen1
    on Dec 9, 2019
    More actions

    All too eager to start using the ?. syntax I managed to play a trick on myself by converting
    if (foo && foo.bar && foo.bar.indexOf(someValue) !== -1)
    to
    if (foo?.bar?.indexOf(someValue) !== -1)
    which had quite the opposite behaviour of what was intended heh.

    Just one of the (probably more obvious in hindsight) things a script would have to keep in mind :)

  13. Daniel Rosenwasser (DanielRosenwasser)
    changed the title [-]Convert && chain to optional chain expression[/-] [+]Refactoring to convert && chain to optional chain expression[/+] on Apr 22, 2020
  14. Ryan Cavanaugh (RyanCavanaugh)
    assigned
    Mine Starks (minestarks)
    and unassigned
    Daniel Rosenwasser (DanielRosenwasser)
    on May 4, 2020
  15. Ryan Cavanaugh (RyanCavanaugh)
    modified the milestones: Backlog, TypeScript 4.0 on May 4, 2020
  16. Ryan Cavanaugh (RyanCavanaugh)
    added
    CommittedThe team has roadmapped this issueThe team has roadmapped this issue
    on May 4, 2020
  17. Mine Starks (minestarks)
    assigned
    Jesse Trinity (jessetrinity)
    on May 4, 2020
  18. DanielRosenwasser commented on May 5, 2020

    @DanielRosenwasser
    Daniel Rosenwasser (DanielRosenwasser)
    on May 5, 2020
    MemberAuthor
    More actions

    There's a question of whether or not nullish coalescing (??) should be supported here as part of the code action.

  19. DanielRosenwasser commented on May 5, 2020

    @DanielRosenwasser
    Daniel Rosenwasser (DanielRosenwasser)
    on May 5, 2020
    MemberAuthor
    More actions

    Also, what the supported patterns are:

    Category Input Output Comments
    && property chains: a && a.b.c && a.b.c.d a?.b.c?.d
    Ternary checks with property access a.b ? a.b.c : "hello" a.b?.c ?? "hello" Is this one type-driven? What if c is string | null?
    && property/call chains: a && a.b.c && a.b.c() a?.b.c?.()
  20. Kingwl commented on May 13, 2020

    @Kingwl
    Wenlu Wang (Kingwl)
    on May 13, 2020
    Contributor
    More actions

    Ahhhh, i have some existed work about this. And I'm happy to port here

    Reacted by Kevin Tan
  21. kylemh commented on May 13, 2020

    @kylemh
    Kyle Holmberg (kylemh)
    on May 13, 2020
    More actions

    I’m not sure of the viability of going after library refactorings (as opposed to just JS -> TS), but I think one huge win would be if TS could automatically refactor lodash/get usages and/or ramda/pathOr usages.

  22. Kingwl commented on May 13, 2020

    @Kingwl
    Wenlu Wang (Kingwl)
    on May 13, 2020
    Contributor
    More actions

    if TS could automatically refactor lodash/get usages and/or ramda/pathOr usages.

    Good idea. IMO TypeScript cannot provide that but there's outside tools.
    eg: https://github.com/HearTao/ts-upgrade

    Reacted by Kyle Holmberg, ZHAO Jin-Xiang and Ebuall
  23. Kingwl commented on May 20, 2020

    @Kingwl
    Wenlu Wang (Kingwl)
    on May 20, 2020
    Contributor
    More actions

    Are there any existed tools or algorithms to compare expression and judge they are equality?
    I have some ugly work about it but they cannot work well.

  24. Ryan Cavanaugh (RyanCavanaugh)
    modified the milestones: TypeScript 4.0, Typescript 4.0.1 on Jun 29, 2020
  25. Jesse Trinity (jessetrinity)
    closed this as completedin #39135on Jul 14, 2020
  26. TypeScript Bot (typescript-bot)
    added
    Fix AvailableA PR has been opened for this issueA PR has been opened for this issue
    on Nov 6, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

  • @jessetrinity
    Jesse Trinity (jessetrinity)
  • @minestarks
    Mine Starks (minestarks)

Labels

CommittedThe team has roadmapped this issueThe team has roadmapped this issueDomain: LS: Refactoringse.g. extract to constant or function, rename symbole.g. extract to constant or function, rename symbolFix AvailableA PR has been opened for this issueA PR has been opened for this issueSuggestionAn idea for TypeScriptAn idea for TypeScript

Type

No type

Projects

No projects

    Milestone

    • TypeScript 4.0.1

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

    You are visitor 01402503 — GeezerVison does not store the pages you look up.
    Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.