import { assertMethodDecorator, isDecoratorCall, } from "../common/decorators.js"; import type { Method } from "../common/types.js"; import { resolveCallable } from "../common/utils.js"; type BindDecorator = ( value: Method, context: ClassMethodDecoratorContext>, ) => void; export function bind( value: Method, context: ClassMethodDecoratorContext>, ): void; export function bind(): BindDecorator; export function bind(inputOrValue?: unknown, context?: unknown): unknown { const decorate: BindDecorator = function( value: Method, decoratorContext: ClassMethodDecoratorContext>, ): void { assertMethodDecorator("bind", value, decoratorContext); const methodName = decoratorContext.name; decoratorContext.addInitializer(function(this: This): void { (this as Record)[methodName as PropertyKey] = resolveCallable( this, methodName as keyof This, ) as Method; }); }; if (arguments.length === 2 && isDecoratorCall(context)) { return decorate( inputOrValue as Method, context as ClassMethodDecoratorContext>, ); } return decorate; }