You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Destructuring strings is supported by TS but not by TSTL/Lua #1429
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.
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
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.
Activity
[-]Restructuring strings is supported by TS but not by TSTL/Lua[/-][+]Destructuring strings is supported by TS but not by TSTL/Lua[/+]lolleko commentedon Apr 10, 2023
Related to #590
Perryvw commentedon Apr 11, 2023
What is the proposed output for your example:
Something like this?
lolleko commentedon 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 commentedon 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
lolleko commentedon 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.unpackwhich wont work on string types.