Skip to content

Commit 48956c1

Browse files
committed
feat: improve pcall and xpcall types
1 parent 54ef15c commit 48956c1

3 files changed

Lines changed: 13 additions & 3 deletions

File tree

5.1.d.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,10 @@ declare function unpack(list: any[], i?: number, j?: number): any[];
5858
*
5959
* xpcall calls function f in protected mode, using err as the error handler. Any error inside f is not propagated; instead, xpcall catches the error, calls the err function with the original error object, and returns a status code. Its first result is the status code (a boolean), which is true if the call succeeds without errors. In this case, xpcall also returns all results from the call, after this first result. In case of any error, xpcall returns false plus the result from err.
6060
*/
61-
declare function xpcall(f: () => any, err: () => any): true | [false, string];
61+
declare function xpcall<R, E>(
62+
f: (...args: any[]) => R,
63+
err: (err: any) => E,
64+
): [true, R] | [false, E];
6265

6366
/**
6467
* Returns the current environment in use by the function. f can be a Lua function or a number that specifies the function at that stack level: Level 1 is the function calling getfenv. If the given function is not a Lua function, or if f is 0, getfenv returns the global environment. The default for f is 1.

core/global.d.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,10 @@ declare function pairs<T>(t: T): [(t: T, index?: keyof any) => [keyof any, any],
114114
* Calls function f with the given arguments in protected mode. This means that any error inside f is not propagated; instead, pcall catches the error and returns a status code. Its first result is the status code (a boolean), which is true if the call succeeds without errors. In such case, pcall also returns all results from the call, after this first result. In case of any error, pcall returns false plus the error message.
115115
* @tupleReturn
116116
*/
117-
declare function pcall(f: () => any, ...args: any[]): true | [false, string];
117+
declare function pcall<Args extends any[], R>(
118+
f: (...args: Args) => R,
119+
...args: Args
120+
): [true, R] | [false, string];
118121

119122
/**
120123
* Receives any number of arguments and prints their values to stdout, using the tostring function to convert each argument to a string. print is not intended for formatted output, but only as a quick way to show a value, for instance for debugging. For complete control over the output, use string.format and io.write.

extra/5.2-plus.d.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,11 @@ declare function loadfile(
4343
/**
4444
* This function is similar to pcall, except that it sets a new message handler msgh.
4545
*/
46-
declare function xpcall(f: () => any, msgh: () => any, ...args: any[]): true | [false, string];
46+
declare function xpcall<Args extends any[], R, E>(
47+
f: (...args: Args) => R,
48+
msgh: (err: any) => E,
49+
...args: Args
50+
): [true, R] | [false, E];
4751

4852
/**
4953
* Creates a module. If there is a table in package.loaded[name], this table is the module. Otherwise, if there is a global table t with the given name, this table is the module. Otherwise creates a new table t and sets it as the value of the global name and the value of package.loaded[name]. This function also initializes t._NAME with the given name, t._M with the module (t itself), and t._PACKAGE with the package name (the full module name minus last component; see below). Finally, module sets t as the new environment of the current function and the new value of package.loaded[name], so that require returns t.

0 commit comments

Comments
 (0)