Skip to content

Commit e2283dc

Browse files
authored
declaration fixes (#25)
* updated assert to handle return values * updated getmetatable to allow any type as input, including strings which have a metatable * fixed setmetatable index metamethod handling * added support for LuaTables to pairs * added missing string.rep function * added tests for io and file * fixed popen return values * fixes to io read functions * improved declarations for io read * added standard stream handles to io * fixed mistake in file.lines Co-authored-by: Tom <[email protected]>
1 parent 7fff118 commit e2283dc

File tree

13 files changed

+1417
-21
lines changed

13 files changed

+1417
-21
lines changed

core/global.d.ts

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,11 @@ declare const _G: typeof globalThis;
2525
* otherwise, returns all its arguments. In case of error, `message` is the
2626
* error object; when absent, it defaults to "assertion failed!"
2727
*/
28-
declare function assert(v: any, message?: string): asserts v;
28+
declare function assert<V>(v: V): Exclude<V, undefined | null | false>;
29+
declare function assert<V, A extends any[]>(
30+
v: V,
31+
...args: A
32+
): LuaMultiReturn<[Exclude<V, undefined | null | false>, ...A]>;
2933

3034
/**
3135
* This function is a generic interface to the garbage collector. It performs
@@ -109,7 +113,7 @@ declare function error(message: string, level?: number): never;
109113
* metatable has a __metatable field, returns the associated value. Otherwise,
110114
* returns the metatable of the given object.
111115
*/
112-
declare function getmetatable<T extends object>(object: T): LuaMetatable<T> | undefined;
116+
declare function getmetatable<T>(object: T): LuaMetatable<T> | undefined;
113117

114118
/**
115119
* Returns three values (an iterator function, the table t, and 0) so that the
@@ -153,6 +157,9 @@ declare function next(table: object, index?: any): LuaMultiReturn<[any, any] | [
153157
* See function next for the caveats of modifying the table during its
154158
* traversal.
155159
*/
160+
declare function pairs<TKey, TValue>(
161+
t: LuaTable<TKey, TValue>
162+
): LuaIterable<LuaMultiReturn<[TKey, TValue]>>;
156163
declare function pairs<T>(t: T): LuaIterable<LuaMultiReturn<[keyof T, T[keyof T]]>>;
157164

158165
/**
@@ -234,10 +241,17 @@ declare function select<T>(index: '#', ...args: T[]): number;
234241
*
235242
* This function returns table.
236243
*/
237-
declare function setmetatable<T extends object, TIndex extends object>(
244+
declare function setmetatable<
245+
T extends object,
246+
TIndex extends object | ((this: T, key: any) => any) | undefined = undefined
247+
>(
238248
table: T,
239-
metatable: LuaMetatable<T & TIndex, TIndex> | null | undefined
240-
): T & TIndex;
249+
metatable?: LuaMetatable<T, TIndex> | null
250+
): TIndex extends (this: T, key: infer TKey) => infer TValue
251+
? T & { [K in TKey & string]: TValue }
252+
: TIndex extends object
253+
? T & TIndex
254+
: T;
241255

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

core/io.d.ts

Lines changed: 30 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -61,9 +61,7 @@ declare namespace io {
6161
filename?: string,
6262
...formats: T
6363
): LuaIterable<
64-
LuaMultiReturn<
65-
[] extends T ? [string] : { [P in keyof T]: T[P] extends 'n' ? number : string }
66-
>
64+
LuaMultiReturn<[] extends T ? [string] : { [P in keyof T]: FileReadFormatToType<T[P]> }>
6765
>;
6866

6967
/**
@@ -100,12 +98,31 @@ declare namespace io {
10098
* you can use to read data from this program (if mode is "r", the default) or
10199
* to write data to this program (if mode is "w").
102100
*/
103-
function popen(prog: string, mode?: 'r' | 'w'): LuaFile;
101+
function popen(prog: string, mode?: 'r' | 'w'): LuaMultiReturn<[LuaFile] | [undefined, string]>;
104102

105103
/**
106104
* Equivalent to io.input():read(···).
107105
*/
108-
const read: LuaFile['read'];
106+
function read(): io.FileReadFormatToType<io.FileReadLineFormat> | undefined;
107+
function read<T extends io.FileReadFormat>(format: T): io.FileReadFormatToType<T> | undefined;
108+
function read<T extends io.FileReadFormat[]>(
109+
...formats: T
110+
): LuaMultiReturn<{ [P in keyof T]?: io.FileReadFormatToType<T[P]> }>;
111+
112+
/**
113+
* Predefined file handle for standard error stream. The I/O library never closes this file.
114+
*/
115+
const stderr: LuaFile;
116+
117+
/**
118+
* Predefined file handle for standard input stream. The I/O library never closes this file.
119+
*/
120+
const stdin: LuaFile;
121+
122+
/**
123+
* Predefined file handle for standard output stream. The I/O library never closes this file.
124+
*/
125+
const stdout: LuaFile;
109126

110127
/**
111128
* In case of success, returns a handle for a temporary file. This file is
@@ -125,6 +142,8 @@ declare namespace io {
125142
* Equivalent to io.output():write(···).
126143
*/
127144
function write(...args: (string | number)[]): LuaMultiReturn<[LuaFile] | [undefined, string]>;
145+
146+
type FileReadFormatToType<T> = T extends FileReadNumberFormat ? number : string;
128147
}
129148

130149
interface LuaFile {
@@ -157,12 +176,10 @@ interface LuaFile {
157176
* In case of errors this function raises the error, instead of returning an
158177
* error code.
159178
*/
160-
lines<T extends FileReadFormat[]>(
179+
lines<T extends io.FileReadFormat[]>(
161180
...formats: T
162181
): LuaIterable<
163-
LuaMultiReturn<
164-
[] extends T ? [string] : { [P in keyof T]: T[P] extends 'n' ? number : string }
165-
>
182+
LuaMultiReturn<[] extends T ? [string] : { [P in keyof T]: io.FileReadFormatToType<T[P]> }>
166183
>;
167184

168185
/**
@@ -193,9 +210,11 @@ interface LuaFile {
193210
*
194211
* The formats "l" and "L" should be used only for text files.
195212
*/
196-
read<T extends FileReadFormat[]>(
213+
read(): io.FileReadFormatToType<io.FileReadLineFormat> | undefined;
214+
read<T extends io.FileReadFormat>(format: T): io.FileReadFormatToType<T> | undefined;
215+
read<T extends io.FileReadFormat[]>(
197216
...formats: T
198-
): LuaMultiReturn<{ [P in keyof T]?: T[P] extends 'n' ? number : string }>;
217+
): LuaMultiReturn<{ [P in keyof T]?: io.FileReadFormatToType<T[P]> }>;
199218

200219
/**
201220
* Sets and geionts the file position, measured from the beginning of the

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, TIndex = object> {
3+
interface LuaMetatable<T, TIndex extends object | ((this: T, key: any) => any) | undefined> {
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, TIndex = object> {
102102
* this table with key. (This indexing is regular, not raw, and therefore can
103103
* trigger another metamethod.)
104104
*/
105-
__index?: TIndex | ((this: T, key: any, value: any) => any);
105+
__index?: TIndex;
106106

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

core/string.d.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,11 @@ declare namespace string {
179179
*/
180180
function match(s: string, pattern: string, init?: number): LuaMultiReturn<string[]>;
181181

182+
/**
183+
* Returns a string that is the concatenation of `n` copies of the string `s`.
184+
*/
185+
function rep(s: string, n: number): string;
186+
182187
/**
183188
* Returns a string that is the string s reversed.
184189
*/

special/5.1-only.d.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,4 +96,8 @@ declare namespace debug {
9696
): LuaMultiReturn<[string, any]>;
9797
}
9898

99-
type FileReadFormat = '*n' | '*a' | '*l' | number;
99+
declare namespace io {
100+
type FileReadNumberFormat = '*n';
101+
type FileReadLineFormat = '*l';
102+
type FileReadFormat = FileReadNumberFormat | FileReadLineFormat | '*a' | number;
103+
}

special/5.2-or-jit.d.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,5 @@
1-
type FileReadFormat = '*n' | '*a' | '*l' | '*L' | number;
1+
declare namespace io {
2+
type FileReadNumberFormat = '*n';
3+
type FileReadLineFormat = '*l';
4+
type FileReadFormat = FileReadNumberFormat | FileReadLineFormat | '*a' | '*L' | number;
5+
}

special/5.3-plus.d.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -227,4 +227,8 @@ interface LuaMetatable<T> {
227227
__shr?(this: T, operand: any): any;
228228
}
229229

230-
type FileReadFormat = 'n' | 'a' | 'l' | 'L' | number;
230+
declare namespace io {
231+
type FileReadNumberFormat = 'n';
232+
type FileReadLineFormat = 'l';
233+
type FileReadFormat = FileReadNumberFormat | FileReadLineFormat | 'a' | 'L' | number;
234+
}

0 commit comments

Comments
 (0)