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
Copy file name to clipboardExpand all lines: docs/caveats.md
+17-9Lines changed: 17 additions & 9 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -55,19 +55,27 @@ We recommend that you use the [`eqeqeq`](https://eslint.org/docs/latest/rules/eq
55
55
56
56
`nil` is the Lua equivalent for `undefined`, so TSTL converts `undefined` to `nil`. However, there is no Lua equivlanet for `null`, so TSTL converts `null` to `nil` as well.
57
57
58
-
This means that TSTL programs with `null` will have different behavior than JavaScript/TypeScript programs. For example:
58
+
In most TypeScript programs, you can use `null`and `undefined` interchangably. For this reason, we recommend keeping `null` out of your TSTL codebases in favor of `undefined`. Not only will this represent the transpiled Lua code better, but [it is more idiomatic in TypeScript to prefer `undefined` over `null` when both would accomplish the same thing](https://basarat.gitbook.io/typescript/recap/null-undefined).
59
59
60
-
```ts
61
-
const foo = {
62
-
someProp1: 123,
63
-
someProp2: null,
64
-
someProp3: undefined,
65
-
};
60
+
### Table Key Deletion & Existence
61
+
62
+
In JavaScript, object keys can exist with any value, including `undefined` and `null`. For example:
63
+
64
+
```js
65
+
constfoo= {};
66
+
foo.someProp1=123;
67
+
foo.someProp2=undefined;
68
+
foo.someProp3=null;
69
+
for (constkeyofObject.keys(foo)) {
70
+
console.log(key);
71
+
}
66
72
```
67
73
68
-
If we iterated over `foo` in a TSTL program, we would _only_ get `someProp1`, instead of both `someProp1` and `someProp2` like we would in a JavaScript/TypeScript program.
74
+
This code will print out all 3 keys. In JavaScript, if you want to get rid of an object key, then you have to use the special `delete` operator (e.g. `delete foo.someProp3`).
75
+
76
+
Lua does not have a special `delete` operator. Instead, in Lua, table keys are deleted by assigning a value of `nil` to the key (e.g. `foo.someProp3 = nil`). Since both `undefined` and `null` transpile to `nil`, this means that if we ran the above code in a TSTL program, instead of printing out all 3 keys, only the first key would be printed out.
69
77
70
-
In general, we recommend keeping `null`out of your TSTL codebases in favor of `undefined`. Not only will this represent the transpiled Lua code better, but [it is more idiomatic in TypeScript to prefer `undefined` over `null` when both would accomplish the same thing](https://basarat.gitbook.io/typescript/recap/null-undefined).
78
+
In most cases, this difference should not cause any problems. However, if you are using `null`or `undefined` to represent an initialized zero-value inside of your object, and then you need to read the keys of that object later on, then you will have a problem. To work around this, you could use a value of `-1` or `"__TSTL_NULL"` instead of `null`. (You could also use something like `const Null = {}`.)
0 commit comments