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
Comments
Related to #590 |
What is the proposed output for your example: local __temp = foo()
local a = temp[0]
local b = temp[1] Something like this? |
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. |
Come to think of it, indeed we should be able to use exactly the same code path as we use for arrays |
Yes for literal variable declarations (and assignemnts), we can just treat them the same as array literals. However, for destructured assignment, we use |
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
The text was updated successfully, but these errors were encountered: