Destructuring strings is supported by TS but not by TSTL/Lua · Issue #1429 · TypeScriptToLua/TypeScriptToLua · GitHub
Skip to content

Destructuring strings is supported by TS but not by TSTL/Lua #1429

Open

Description

@lolleko

Because strings are iterable, TS supports destructuring them into individual characters:

const [a, b] = "test"; // a = "t" ; b = "e";

We currently do not support that in TSTL.
We should think about adding support for handling strings in restructuring assignments. At the very least, we should throw a diagnostic that this currently does not work.

Current behavior:

https://typescripttolua.github.io/play/#code/GYVwdgxgLglg9mABMOcAUUAWMDOAuRANzhgBMBKAnKAJxjAHNEBvAKEQ8RoFMoQakAIijdqggNysAvq1YQE1RAG0AhgBpEAIwC6iALzJUacpNYAHOmChp1Wk0A

Epected behaviour a = "t", b = "e";

https://www.typescriptlang.org/play?#code/GYVwdgxgLglg9mABMOcAUUAWMDOAuRANzhgBMBKAnKAJxjAHNEBvAKEQ8RoFMoQakAIijdqggNysAvq1YQE1RAG0AhgBpEAIwC6iALzJUacpLkK4AG24A6C3AZp1Wk0A

Discovered while fixing #1411

Activity

  1. changed the title [-]Restructuring strings is supported by TS but not by TSTL/Lua[/-] [+]Destructuring strings is supported by TS but not by TSTL/Lua[/+] on Apr 10, 2023
  2. lolleko commented on Apr 10, 2023

    @lolleko
    MemberAuthor

    Related to #590

  3. Perryvw commented on Apr 11, 2023

    @Perryvw
    Member

    What is the proposed output for your example:

    local __temp = foo()
    local a = temp[0]
    local b = temp[1]

    Something like this?

  4. lolleko commented on Apr 11, 2023

    @lolleko
    MemberAuthor

    Modern JS (don't know which standard maybe es5?) actually has built in support for string/iterator destructuring.

    For older JS targets TS transpiles to pretty much what you recommended. So we can probably get away with treating strings the same as arrays/tuples.

  5. Perryvw commented on Apr 11, 2023

    @Perryvw
    Member

    Come to think of it, indeed we should be able to use exactly the same code path as we use for arrays

  6. lolleko commented on Apr 11, 2023

    @lolleko
    MemberAuthor

    Yes for literal variable declarations (and assignemnts), we can just treat them the same as array literals. However, for destructured assignment, we use table.unpack which wont work on string types.

  7. linked a pull request that will close this issueString destructuring #1430on Apr 11, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions