Skip to content

Commit a935480

Browse files
hazzard993ark120202
authored andcommitted
feat(metatable): use __index in setmetatable return type (#9)
* fix: join both metatable object types * fix: use metatable and prototype to identify this * Make second `LuaMetatable` type parameter optional * Rename second `LuaMetatable` type parameter * Update `debug.setmetatable` type
1 parent c725258 commit a935480

File tree

3 files changed

+9
-6
lines changed

3 files changed

+9
-6
lines changed

core/debug.d.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -192,7 +192,10 @@ declare namespace debug {
192192
* Sets the metatable for the given value to the given table (which can be
193193
* nil). Returns value.
194194
*/
195-
function setmetatable<T>(value: T, table: LuaMetatable<T> | null | undefined): T;
195+
function setmetatable<T, TIndex>(
196+
value: T,
197+
table: LuaMetatable<T & TIndex, TIndex> | null | undefined,
198+
): T & TIndex;
196199

197200
/**
198201
* This function assigns the value value to the upvalue with index up of the

core/global.d.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -245,10 +245,10 @@ declare function select<T>(index: '#', ...args: T[]): number;
245245
*
246246
* This function returns table.
247247
*/
248-
declare function setmetatable<T extends object>(
248+
declare function setmetatable<T extends object, TIndex extends object>(
249249
table: T,
250-
metatable: LuaMetatable<T> | null | undefined,
251-
): T;
250+
metatable: LuaMetatable<T & TIndex, TIndex> | null | undefined,
251+
): T & TIndex;
252252

253253
/**
254254
* When called with no base, tonumber tries to convert its argument to a number.

core/metatable.d.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// Based on https://www.lua.org/manual/5.3/manual.html#2.4
22

3-
interface LuaMetatable<T> {
3+
interface LuaMetatable<T, TIndex = object> {
44
/**
55
* the addition (+) operation. If any operand for an addition is not a number
66
* (nor a string coercible to a number), Lua will try to call a metamethod.
@@ -102,7 +102,7 @@ interface LuaMetatable<T> {
102102
* this table with key. (This indexing is regular, not raw, and therefore can
103103
* trigger another metamethod.)
104104
*/
105-
__index?: object | ((this: T, key: any, value: any) => any);
105+
__index?: TIndex | ((this: T, key: any, value: any) => any);
106106

107107
/**
108108
* The indexing assignment table[key] = value. Like the index event, this

0 commit comments

Comments
 (0)