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 strings is supported by TS but not by TSTL/Lua #1429

Open
lolleko opened this issue Apr 10, 2023 · 5 comments · May be fixed by #1430
Open

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

lolleko opened this issue Apr 10, 2023 · 5 comments · May be fixed by #1430
Labels
enhancement scope: transformation Transformation of TS to Lua

Comments

@lolleko
Copy link
Member

lolleko commented Apr 10, 2023

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

@lolleko lolleko 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 Apr 10, 2023
@lolleko
Copy link
Member Author

lolleko commented Apr 10, 2023

Related to #590

@Perryvw
Copy link
Member

Perryvw commented Apr 11, 2023

What is the proposed output for your example:

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

Something like this?

@lolleko
Copy link
Member Author

lolleko commented Apr 11, 2023

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.

@Perryvw
Copy link
Member

Perryvw commented Apr 11, 2023

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

@Perryvw Perryvw added enhancement scope: transformation Transformation of TS to Lua labels Apr 11, 2023
@lolleko
Copy link
Member Author

lolleko commented Apr 11, 2023

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.

@lolleko lolleko linked a pull request Apr 11, 2023 that will close this issue
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement scope: transformation Transformation of TS to Lua
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants