From 4042530424b95a7ed0b29204f40a26eb29b278d9 Mon Sep 17 00:00:00 2001 From: Xinquan XU Date: Wed, 21 Jun 2023 14:28:03 +0800 Subject: [PATCH] fix: introduce CallableFunction and NewableFunction from typescript/lib/lib.es5.d.ts --- NOTICE | 1 + std/assembly/index.d.ts | 84 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 85 insertions(+) diff --git a/NOTICE b/NOTICE index a9ddc72e21..4962c2400e 100644 --- a/NOTICE +++ b/NOTICE @@ -55,6 +55,7 @@ under the licensing terms detailed in LICENSE: * CountBleck * Abdul Rauf * Bach Le +* Xinquan Xu Portions of this software are derived from third-party works licensed under the following terms: diff --git a/std/assembly/index.d.ts b/std/assembly/index.d.ts index 52a5e22967..4dbee416a8 100644 --- a/std/assembly/index.d.ts +++ b/std/assembly/index.d.ts @@ -2307,6 +2307,90 @@ interface Function { /** Returns a string representation of this function. */ toString(): string; } + +/** + * Extracts the type of the 'this' parameter of a function type, or 'unknown' if the function type has no 'this' parameter. + */ +type ThisParameterType = T extends (this: infer U, ...args: never) => any ? U : unknown; + +/** + * Removes the 'this' parameter from a function type. + */ +type OmitThisParameter = unknown extends ThisParameterType ? T : T extends (...args: infer A) => infer R ? (...args: A) => R : T; + +interface CallableFunction extends Function { + /** + * Calls the function with the specified object as the this value and the elements of specified array as the arguments. + * @param thisArg The object to be used as the this object. + */ + apply(this: (this: T) => R, thisArg: T): R; + + /** + * Calls the function with the specified object as the this value and the elements of specified array as the arguments. + * @param thisArg The object to be used as the this object. + * @param args An array of argument values to be passed to the function. + */ + apply(this: (this: T, ...args: A) => R, thisArg: T, args: A): R; + + /** + * Calls the function with the specified object as the this value and the specified rest arguments as the arguments. + * @param thisArg The object to be used as the this object. + * @param args Argument values to be passed to the function. + */ + call(this: (this: T, ...args: A) => R, thisArg: T, ...args: A): R; + + /** + * For a given function, creates a bound function that has the same body as the original function. + * The this object of the bound function is associated with the specified object, and has the specified initial parameters. + * @param thisArg The object to be used as the this object. + */ + bind(this: T, thisArg: ThisParameterType): OmitThisParameter; + + /** + * For a given function, creates a bound function that has the same body as the original function. + * The this object of the bound function is associated with the specified object, and has the specified initial parameters. + * @param thisArg The object to be used as the this object. + * @param args Arguments to bind to the parameters of the function. + */ + bind(this: (this: T, ...args: [...A, ...B]) => R, thisArg: T, ...args: A): (...args: B) => R; +} + +interface NewableFunction extends Function { + /** + * Calls the function with the specified object as the this value and the elements of specified array as the arguments. + * @param thisArg The object to be used as the this object. + */ + apply(this: new () => T, thisArg: T): void; + /** + * Calls the function with the specified object as the this value and the elements of specified array as the arguments. + * @param thisArg The object to be used as the this object. + * @param args An array of argument values to be passed to the function. + */ + apply(this: new (...args: A) => T, thisArg: T, args: A): void; + + /** + * Calls the function with the specified object as the this value and the specified rest arguments as the arguments. + * @param thisArg The object to be used as the this object. + * @param args Argument values to be passed to the function. + */ + call(this: new (...args: A) => T, thisArg: T, ...args: A): void; + + /** + * For a given function, creates a bound function that has the same body as the original function. + * The this object of the bound function is associated with the specified object, and has the specified initial parameters. + * @param thisArg The object to be used as the this object. + */ + bind(this: T, thisArg: any): T; + + /** + * For a given function, creates a bound function that has the same body as the original function. + * The this object of the bound function is associated with the specified object, and has the specified initial parameters. + * @param thisArg The object to be used as the this object. + * @param args Arguments to bind to the parameters of the function. + */ + bind(this: new (...args: [...A, ...B]) => R, thisArg: any, ...args: A): new (...args: B) => R; +} + interface IArguments {} interface RegExp {}