Skip to content

Commit bced8a3

Browse files
authored
Update caveats.md (#154)
* Update caveats.md * Update caveats.md
1 parent 87dc036 commit bced8a3

File tree

1 file changed

+17
-9
lines changed

1 file changed

+17
-9
lines changed

docs/caveats.md

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -55,19 +55,27 @@ We recommend that you use the [`eqeqeq`](https://eslint.org/docs/latest/rules/eq
5555

5656
`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.
5757

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).
5959

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+
const foo = {};
66+
foo.someProp1 = 123;
67+
foo.someProp2 = undefined;
68+
foo.someProp3 = null;
69+
for (const key of Object.keys(foo)) {
70+
console.log(key);
71+
}
6672
```
6773

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.
6977

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 = {}`.)
7179

7280
### Array Length
7381

0 commit comments

Comments
 (0)