opusdev/vector-similarity-api
1
1type Intrinsic = typeof globalThis;2 3type IntrinsicName = keyof Intrinsic | `%${keyof Intrinsic}%`;4 5type IntrinsicPath = IntrinsicName | `${StripPercents<IntrinsicName>}.${string}` | `%${StripPercents<IntrinsicName>}.${string}%`;6 7type AllowMissing = boolean;8 9type StripPercents<T extends string> = T extends `%${infer U}%` ? U : T;10 11type BindMethodPrecise<F> =12 F extends (this: infer This, ...args: infer Args) => infer R13 ? (obj: This, ...args: Args) => R14 : F extends {15 (this: infer This1, ...args: infer Args1): infer R1;16 (this: infer This2, ...args: infer Args2): infer R217 }18 ? {19 (obj: This1, ...args: Args1): R1;20 (obj: This2, ...args: Args2): R221 }22 : never23 24// Extract method type from a prototype25type 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 : never30 : never31 32// Get static property/method33type GetStaticMember<T extends keyof typeof globalThis, P extends string> =34 P extends keyof (typeof globalThis)[T] ? (typeof globalThis)[T][P] : never35 36// Type that maps string path to actual bound function or value with better precision37type BoundIntrinsic<S extends string> =38 S extends `${infer Obj}.prototype.${infer Method}`39 ? Obj extends keyof typeof globalThis40 ? BindMethodPrecise<GetPrototypeMethod<Obj, Method & string>>41 : unknown42 : S extends `${infer Obj}.${infer Prop}`43 ? Obj extends keyof typeof globalThis44 ? GetStaticMember<Obj, Prop & string>45 : unknown46 : unknown47 48declare function arraySlice<T>(array: readonly T[], start?: number, end?: number): T[];49declare function arraySlice<T>(array: ArrayLike<T>, start?: number, end?: number): T[];50declare function arraySlice<T>(array: IArguments, start?: number, end?: number): T[];51 52// Special cases for methods that need explicit typing53interface 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): string58 };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 | undefined82 ) => 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 */91declare function callBound<K extends keyof SpecialCases | StripPercents<keyof SpecialCases>, S extends IntrinsicPath>(name: K, allowMissing?: AllowMissing): SpecialCases[`%${StripPercents<K>}%`];92declare function callBound<K extends keyof SpecialCases | StripPercents<keyof SpecialCases>, S extends IntrinsicPath>(name: S, allowMissing?: AllowMissing): BoundIntrinsic<S>;93 94export = callBound;95 