| 1 | type Intrinsic = typeof globalThis;
|
|---|
| 2 |
|
|---|
| 3 | type IntrinsicName = keyof Intrinsic | `%${keyof Intrinsic}%`;
|
|---|
| 4 |
|
|---|
| 5 | type IntrinsicPath = IntrinsicName | `${StripPercents<IntrinsicName>}.${string}` | `%${StripPercents<IntrinsicName>}.${string}%`;
|
|---|
| 6 |
|
|---|
| 7 | type AllowMissing = boolean;
|
|---|
| 8 |
|
|---|
| 9 | type StripPercents<T extends string> = T extends `%${infer U}%` ? U : T;
|
|---|
| 10 |
|
|---|
| 11 | type BindMethodPrecise<F> =
|
|---|
| 12 | F extends (this: infer This, ...args: infer Args) => infer R
|
|---|
| 13 | ? (obj: This, ...args: Args) => R
|
|---|
| 14 | : F extends {
|
|---|
| 15 | (this: infer This1, ...args: infer Args1): infer R1;
|
|---|
| 16 | (this: infer This2, ...args: infer Args2): infer R2
|
|---|
| 17 | }
|
|---|
| 18 | ? {
|
|---|
| 19 | (obj: This1, ...args: Args1): R1;
|
|---|
| 20 | (obj: This2, ...args: Args2): R2
|
|---|
| 21 | }
|
|---|
| 22 | : never
|
|---|
| 23 |
|
|---|
| 24 | // Extract method type from a prototype
|
|---|
| 25 | type GetPrototypeMethod<T extends keyof typeof globalThis, M extends string> =
|
|---|
| 26 | (typeof globalThis)[T] extends { prototype: any }
|
|---|
| 27 | ? M extends keyof (typeof globalThis)[T]['prototype']
|
|---|
| 28 | ? (typeof globalThis)[T]['prototype'][M]
|
|---|
| 29 | : never
|
|---|
| 30 | : never
|
|---|
| 31 |
|
|---|
| 32 | // Get static property/method
|
|---|
| 33 | type GetStaticMember<T extends keyof typeof globalThis, P extends string> =
|
|---|
| 34 | P extends keyof (typeof globalThis)[T] ? (typeof globalThis)[T][P] : never
|
|---|
| 35 |
|
|---|
| 36 | // Type that maps string path to actual bound function or value with better precision
|
|---|
| 37 | type BoundIntrinsic<S extends string> =
|
|---|
| 38 | S extends `${infer Obj}.prototype.${infer Method}`
|
|---|
| 39 | ? Obj extends keyof typeof globalThis
|
|---|
| 40 | ? BindMethodPrecise<GetPrototypeMethod<Obj, Method & string>>
|
|---|
| 41 | : unknown
|
|---|
| 42 | : S extends `${infer Obj}.${infer Prop}`
|
|---|
| 43 | ? Obj extends keyof typeof globalThis
|
|---|
| 44 | ? GetStaticMember<Obj, Prop & string>
|
|---|
| 45 | : unknown
|
|---|
| 46 | : unknown
|
|---|
| 47 |
|
|---|
| 48 | declare function arraySlice<T>(array: readonly T[], start?: number, end?: number): T[];
|
|---|
| 49 | declare function arraySlice<T>(array: ArrayLike<T>, start?: number, end?: number): T[];
|
|---|
| 50 | declare function arraySlice<T>(array: IArguments, start?: number, end?: number): T[];
|
|---|
| 51 |
|
|---|
| 52 | // Special cases for methods that need explicit typing
|
|---|
| 53 | interface SpecialCases {
|
|---|
| 54 | '%Object.prototype.isPrototypeOf%': (thisArg: {}, obj: unknown) => boolean;
|
|---|
| 55 | '%String.prototype.replace%': {
|
|---|
| 56 | (str: string, searchValue: string | RegExp, replaceValue: string): string;
|
|---|
| 57 | (str: string, searchValue: string | RegExp, replacer: (substring: string, ...args: any[]) => string): string
|
|---|
| 58 | };
|
|---|
| 59 | '%Object.prototype.toString%': (obj: {}) => string;
|
|---|
| 60 | '%Object.prototype.hasOwnProperty%': (obj: {}, v: PropertyKey) => boolean;
|
|---|
| 61 | '%Array.prototype.slice%': typeof arraySlice;
|
|---|
| 62 | '%Array.prototype.map%': <T, U>(array: readonly T[], callbackfn: (value: T, index: number, array: readonly T[]) => U, thisArg?: any) => U[];
|
|---|
| 63 | '%Array.prototype.filter%': <T>(array: readonly T[], predicate: (value: T, index: number, array: readonly T[]) => unknown, thisArg?: any) => T[];
|
|---|
| 64 | '%Array.prototype.indexOf%': <T>(array: readonly T[], searchElement: T, fromIndex?: number) => number;
|
|---|
| 65 | '%Function.prototype.apply%': <T, A extends any[], R>(fn: (...args: A) => R, thisArg: any, args: A) => R;
|
|---|
| 66 | '%Function.prototype.call%': <T, A extends any[], R>(fn: (...args: A) => R, thisArg: any, ...args: A) => R;
|
|---|
| 67 | '%Function.prototype.bind%': <T, A extends any[], R>(fn: (...args: A) => R, thisArg: any, ...args: A) => (...remainingArgs: A) => R;
|
|---|
| 68 | '%Promise.prototype.then%': {
|
|---|
| 69 | <T, R>(promise: Promise<T>, onfulfilled: (value: T) => R | PromiseLike<R>): Promise<R>;
|
|---|
| 70 | <T, R>(promise: Promise<T>, onfulfilled: ((value: T) => R | PromiseLike<R>) | undefined | null, onrejected: (reason: any) => R | PromiseLike<R>): Promise<R>;
|
|---|
| 71 | };
|
|---|
| 72 | '%RegExp.prototype.test%': (regexp: RegExp, str: string) => boolean;
|
|---|
| 73 | '%RegExp.prototype.exec%': (regexp: RegExp, str: string) => RegExpExecArray | null;
|
|---|
| 74 | '%Error.prototype.toString%': (error: Error) => string;
|
|---|
| 75 | '%TypeError.prototype.toString%': (error: TypeError) => string;
|
|---|
| 76 | '%String.prototype.split%': (
|
|---|
| 77 | obj: unknown,
|
|---|
| 78 | splitter: string | RegExp | {
|
|---|
| 79 | [Symbol.split](string: string, limit?: number): string[];
|
|---|
| 80 | },
|
|---|
| 81 | limit?: number | undefined
|
|---|
| 82 | ) => string[];
|
|---|
| 83 | }
|
|---|
| 84 |
|
|---|
| 85 | /**
|
|---|
| 86 | * Returns a bound function for a prototype method, or a value for a static property.
|
|---|
| 87 | *
|
|---|
| 88 | * @param name - The name of the intrinsic (e.g. 'Array.prototype.slice')
|
|---|
| 89 | * @param {AllowMissing} [allowMissing] - Whether to allow missing intrinsics (default: false)
|
|---|
| 90 | */
|
|---|
| 91 | declare function callBound<K extends keyof SpecialCases | StripPercents<keyof SpecialCases>, S extends IntrinsicPath>(name: K, allowMissing?: AllowMissing): SpecialCases[`%${StripPercents<K>}%`];
|
|---|
| 92 | declare function callBound<K extends keyof SpecialCases | StripPercents<keyof SpecialCases>, S extends IntrinsicPath>(name: S, allowMissing?: AllowMissing): BoundIntrinsic<S>;
|
|---|
| 93 |
|
|---|
| 94 | export = callBound;
|
|---|