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
added operator map types to language extensions (#24)
* added operator map types to language extensions
* fixed link
* ran prettier
* updates from feedback
Co-authored-by: Tom <tomblind@users.noreply.github.com>
Copy file name to clipboardExpand all lines: docs/advanced/language-extensions.md
+76Lines changed: 76 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -60,3 +60,79 @@ function myFunc(self)
60
60
end
61
61
foo, four=myFunc(nil)
62
62
```
63
+
64
+
## Operator Map Types
65
+
66
+
Lua supports overloading operators on types using [metatable methods](https://www.lua.org/manual/5.4/manual.html#2.4) such as `__add`. But, Javascript and Typescript do not support this. In order to use overloaded operators on types that support them, you can declare special mapping functions in TS that will translate to those operators in Lua.
67
+
68
+
A common example of an overloaded operator is addition of a mathematical vector type:
69
+
70
+
```ts
71
+
// Vector type supplied by a library, which supports math operators
72
+
declareinterfaceVector {
73
+
x:number;
74
+
y:number;
75
+
}
76
+
77
+
declareconst a:Vector;
78
+
declareconst b:Vector;
79
+
const result =a+b; // Not allowed in TS
80
+
```
81
+
82
+
To support addition for this type, you can declare a special function:
const result =addVector(a, b); // transpiles to 'result = a + b'
87
+
```
88
+
89
+
The mapping function does not have to be declared as global. For example, you could use declaration merging to declare it as a static function on `Vector`:
There are also special variants for the mapping types that allow you do declare operator overloads as methods:
100
+
101
+
```ts
102
+
declareinterfaceVector {
103
+
add:LuaAdditionMethod<Vector, Vector>;
104
+
}
105
+
const result =a.add(b); // result = a + b
106
+
```
107
+
108
+
Some operators may have a different return type based on their inputs. You can support this by using intersection types. For example, our `Vector` type might overload the multiplication operator to scale by a number, or perform a dot product on two `Vectors`:
Copy file name to clipboardExpand all lines: docs/advanced/writing-declarations.md
+5-1Lines changed: 5 additions & 1 deletion
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -512,7 +512,9 @@ declare module "creator" {
512
512
513
513
Lua supports overloading of mathematical operators such as `+`, `-` or `*`. This is performed using the [metatable methods](https://www.lua.org/manual/5.4/manual.html#2.4)`__add`, `__sub`, `__mul`, `__div`, and `__unm`. Since TypeScript does not support operator overloading in its type system, this feature is hard to replicate. Unfortunately, this is not something that can be fixed properly right now without forking off our custom TypeScript version.
514
514
515
-
However, there is a workaround that works decently: if you declare a type as an intersection type with `number`, it will inherit all mathematical operators. For example:
515
+
However, there are two possible workarounds. The first one is to declare a type as an intersection type with `number`. It will then inherit all mathematical operators. Keep in mind that this is only partially type safe and may require some additional casting.
The second option was added in version [0.38.0](https://github.com/TypeScriptToLua/TypeScriptToLua/blob/master/CHANGELOG.md#0380). You can now use [language extensions](https://typescripttolua.github.io/docs/advanced/language-extensions) that allow declaring special functions which will transpile to operators. This will be completely type safe if the operators are declared correctly. See [Operator Map Types](language-extensions.md#operator-map-types) for more information.
536
+
533
537
### Import and export
534
538
535
539
Using `import` can be important for making sure an _index.d.ts_ file contains all the declarations needed.
0 commit comments