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
deprecated @vararg annotation and added $vararg constant extension (#33)
* deprectaed @vararg annotation and added $vararg constant extension
* added DeprecatedInVersion tag to @vararg annotation
Co-authored-by: Tom <tomblind@users.noreply.github.com>
@@ -306,109 +306,6 @@ declare function myFunction(n: number): [number, number];
306
306
307
307
Note that if any overloaded signature of a function implementation has the annotation, all array/tuple return values will unpacked in the transpiled output.
308
308
309
-
## @vararg
310
-
311
-
**Target elements:**`(declare) interface or type`
312
-
313
-
Indicates that an array-like type represents a Lua vararg expression (`...`) and should be transpiled to that when used in a spread expression. This is useful for forwarding varargs instead of wrapping them in a table and unpacking them.
314
-
315
-
**Example**
316
-
317
-
<SideBySide>
318
-
319
-
```typescript
320
-
function varargWrapUnpack(...args:string[]) {
321
-
console.log(...args);
322
-
}
323
-
```
324
-
325
-
```lua
326
-
functionvarargWrapUnpack(self, ...)
327
-
localargs= ({...})
328
-
print(unpack(args))
329
-
end
330
-
```
331
-
332
-
</SideBySide>
333
-
334
-
<SideBySide>
335
-
336
-
```typescript
337
-
/**@vararg*/
338
-
interfaceVararg<T> extendsArray<T> {}
339
-
340
-
function varargForward(...args:Vararg<string>) {
341
-
console.log(...args);
342
-
}
343
-
```
344
-
345
-
```lua
346
-
functionvarargForward(self, ...)
347
-
print(...))
348
-
end
349
-
```
350
-
351
-
</SideBySide>
352
-
353
-
This can be used to access the file-scope varargs as well.
354
-
355
-
**Example**
356
-
357
-
<SideBySide>
358
-
359
-
```typescript
360
-
declareconst arg:Vararg<string>;
361
-
console.log(...arg);
362
-
const [x, y] = [...arg];
363
-
```
364
-
365
-
```lua
366
-
print(...)
367
-
localx, y=...
368
-
```
369
-
370
-
</SideBySide>
371
-
372
-
To also support tuple-typed rest parameters, you can define the type like this:
function varargForward(...args:Vararg<[string, number]>) {}
381
-
```
382
-
383
-
**_Warning_**
384
-
385
-
TypeScriptToLua does not check that the vararg expression is valid in the context it is used. If the array is used in a spread operation in an invalid context (such as a nested function), a deoptimization will occur.
Indicates that an array-like type represents a Lua vararg expression (`...`) and should be transpiled to that when used in a spread expression. This is useful for forwarding varargs instead of wrapping them in a table and unpacking them.
759
+
760
+
**Example**
761
+
762
+
<SideBySide>
763
+
764
+
```typescript
765
+
function varargWrapUnpack(...args:string[]) {
766
+
console.log(...args);
767
+
}
768
+
```
769
+
770
+
```lua
771
+
functionvarargWrapUnpack(self, ...)
772
+
localargs= ({...})
773
+
print(unpack(args))
774
+
end
775
+
```
776
+
777
+
</SideBySide>
778
+
779
+
<SideBySide>
780
+
781
+
```typescript
782
+
/**@vararg*/
783
+
interfaceVararg<T> extendsArray<T> {}
784
+
785
+
function varargForward(...args:Vararg<string>) {
786
+
console.log(...args);
787
+
}
788
+
```
789
+
790
+
```lua
791
+
functionvarargForward(self, ...)
792
+
print(...))
793
+
end
794
+
```
795
+
796
+
</SideBySide>
797
+
798
+
This can be used to access the file-scope varargs as well.
799
+
800
+
**Example**
801
+
802
+
<SideBySide>
803
+
804
+
```typescript
805
+
declareconst arg:Vararg<string>;
806
+
console.log(...arg);
807
+
const [x, y] = [...arg];
808
+
```
809
+
810
+
```lua
811
+
print(...)
812
+
localx, y=...
813
+
```
814
+
815
+
</SideBySide>
816
+
817
+
To also support tuple-typed rest parameters, you can define the type like this:
function varargForward(...args:Vararg<[string, number]>) {}
826
+
```
827
+
828
+
**_Warning_**
829
+
830
+
TypeScriptToLua does not check that the vararg expression is valid in the context it is used. If the array is used in a spread operation in an invalid context (such as a nested function), a deoptimization will occur.
831
+
832
+
**Example**
833
+
834
+
<SideBySide>
835
+
836
+
```typescript
837
+
function outerFunction(...args:Vararg<string>) {
838
+
function innerFunction() {
839
+
console.log(...args);
840
+
}
841
+
innerFunction();
842
+
}
843
+
```
844
+
845
+
```lua
846
+
functionouterFunction(self, ...)
847
+
localargs= {...}
848
+
localfunctioninnerFunction(self)
849
+
print(unpack(args))
850
+
end
851
+
innerFunction(_G)
852
+
end
853
+
```
854
+
855
+
</SideBySide>
856
+
857
+
**Upgrade Instructions**
858
+
859
+
`@vararg` is no longer required to prevent vararg parameters from being wrapped in a table. The ellipsis operator will now automatically be used if the parameter is used in a spread expression.
860
+
861
+
Example:
862
+
863
+
<SideBySide>
864
+
865
+
```ts
866
+
function varargForward(...args:string[]) {
867
+
console.log(...args);
868
+
}
869
+
```
870
+
871
+
```lua
872
+
functionvarargForward(...)
873
+
print(...)
874
+
end
875
+
```
876
+
877
+
</SideBySide>
878
+
879
+
However, if the parameter is accessed as an array or tuple, it will be wrapped in a table.
880
+
881
+
Example:
882
+
883
+
<SideBySide>
884
+
885
+
```ts
886
+
function varargAccess(...args:string[]) {
887
+
console.log(args[0]);
888
+
}
889
+
```
890
+
891
+
```lua
892
+
functionvarargAccess(...)
893
+
localargs= {...}
894
+
print(args[1])
895
+
end
896
+
```
897
+
898
+
</SideBySide>
899
+
900
+
Note that are a few cases where the parameter will still be wrapped in a table, even if a spread expression is used, in order to generate correctly functioning Lua.
Lua allows use of the ellipsis operator (`...`) to access command line arguments passed when executing a script file. To access this from Typescript, you can use the `$vararg` constant in a spread expression.
328
+
329
+
Example:
330
+
331
+
<SideBySide>
332
+
333
+
```ts
334
+
console.log(...$vararg);
335
+
```
336
+
337
+
```lua
338
+
print(...)
339
+
```
340
+
341
+
</SideBySide>
342
+
343
+
When run:
344
+
345
+
<SideBySide>
346
+
347
+
```
348
+
> lua myscript.lua foo bar
349
+
```
350
+
351
+
```
352
+
foo bar
353
+
```
354
+
355
+
</SideBySide>
356
+
357
+
Use of `$vararg` is only allowed at file scope, and only in a spread expression (`...$vararg`).
0 commit comments