Skip to content

Commit 5b67330

Browse files
authored
Fixed language extensions LuaTable keys required to be tables (#1034)
* Fixed another mistake in language extensions where luatable keys were accidentally required to be tables * update snapshot names
1 parent bfb89e9 commit 5b67330

File tree

3 files changed

+70
-1
lines changed

3 files changed

+70
-1
lines changed

language-extensions/index.d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -535,7 +535,7 @@ declare type LuaTableDeleteMethod<TKey extends AnyNotNil> = ((key: TKey) => bool
535535
* @param TKey The type of the keys used to access the table.
536536
* @param TValue The type of the values stored in the table.
537537
*/
538-
declare interface LuaTable<TKey extends AnyTable = AnyNotNil, TValue = any> {
538+
declare interface LuaTable<TKey extends AnyNotNil = AnyNotNil, TValue = any> {
539539
length: LuaLengthMethod<number>;
540540
get: LuaTableGetMethod<TKey, TValue>;
541541
set: LuaTableSetMethod<TKey, TValue>;

test/unit/language-extensions/__snapshots__/table.spec.ts.snap

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,51 @@
11
// Jest Snapshot v1, https://goo.gl/fbAQLP
22

3+
exports[`LuaTable extension interface LuaTable in strict mode does not accept key type that could be nil ("null"): code 1`] = `
4+
"local ____exports = {}
5+
____exports.__result = {}
6+
return ____exports"
7+
`;
8+
9+
exports[`LuaTable extension interface LuaTable in strict mode does not accept key type that could be nil ("null"): diagnostics 1`] = `"main.ts(1,38): error TS2344: Type 'null' does not satisfy the constraint 'AnyNotNil'."`;
10+
11+
exports[`LuaTable extension interface LuaTable in strict mode does not accept key type that could be nil ("number | undefined"): code 1`] = `
12+
"local ____exports = {}
13+
____exports.__result = {}
14+
return ____exports"
15+
`;
16+
17+
exports[`LuaTable extension interface LuaTable in strict mode does not accept key type that could be nil ("number | undefined"): diagnostics 1`] = `
18+
"main.ts(1,38): error TS2344: Type 'number | undefined' does not satisfy the constraint 'AnyNotNil'.
19+
Type 'undefined' is not assignable to type 'AnyNotNil'."
20+
`;
21+
22+
exports[`LuaTable extension interface LuaTable in strict mode does not accept key type that could be nil ("string | null"): code 1`] = `
23+
"local ____exports = {}
24+
____exports.__result = {}
25+
return ____exports"
26+
`;
27+
28+
exports[`LuaTable extension interface LuaTable in strict mode does not accept key type that could be nil ("string | null"): diagnostics 1`] = `
29+
"main.ts(1,38): error TS2344: Type 'string | null' does not satisfy the constraint 'AnyNotNil'.
30+
Type 'null' is not assignable to type 'AnyNotNil'."
31+
`;
32+
33+
exports[`LuaTable extension interface LuaTable in strict mode does not accept key type that could be nil ("undefined"): code 1`] = `
34+
"local ____exports = {}
35+
____exports.__result = {}
36+
return ____exports"
37+
`;
38+
39+
exports[`LuaTable extension interface LuaTable in strict mode does not accept key type that could be nil ("undefined"): diagnostics 1`] = `"main.ts(1,38): error TS2344: Type 'undefined' does not satisfy the constraint 'AnyNotNil'."`;
40+
41+
exports[`LuaTable extension interface LuaTable in strict mode does not accept key type that could be nil ("unknown"): code 1`] = `
42+
"local ____exports = {}
43+
____exports.__result = {}
44+
return ____exports"
45+
`;
46+
47+
exports[`LuaTable extension interface LuaTable in strict mode does not accept key type that could be nil ("unknown"): diagnostics 1`] = `"main.ts(1,38): error TS2344: Type 'unknown' does not satisfy the constraint 'AnyNotNil'."`;
48+
349
exports[`LuaTableDelete extension LuaTableDelete invalid use as expression ("const foo = [tableDelete({}, \\"foo\\")];"): code 1`] = `
450
"foo = {
551
(function()

test/unit/language-extensions/table.spec.ts

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -254,6 +254,29 @@ describe("LuaTable extension interface", () => {
254254
.expectToEqual(3);
255255
});
256256

257+
// Test to catch issue from https://github.com/TypeScriptToLua/TypeScriptToLua/issues/1033
258+
test("typed table in type declaration", () => {
259+
util.testFunction`
260+
function fill(tbl: LuaTable<string, number>) {
261+
tbl.set("foo", 3);
262+
return tbl;
263+
}
264+
return fill(new LuaTable()).get("foo");
265+
`
266+
.setOptions(tableProjectOptions)
267+
.expectToEqual(3);
268+
});
269+
270+
test.each([["null"], ["undefined"], ["number | undefined"], ["string | null"], ["unknown"]])(
271+
"LuaTable in strict mode does not accept key type that could be nil (%p)",
272+
keyType => {
273+
util.testExpression`new LuaTable<${keyType}, unknown>()`
274+
.setOptions({ ...tableProjectOptions, strict: true })
275+
.expectToHaveDiagnostics()
276+
.expectDiagnosticsToMatchSnapshot();
277+
}
278+
);
279+
257280
test("object keyed table", () => {
258281
util.testFunction`
259282
interface Key { keyStr: string }

0 commit comments

Comments
 (0)