OmitThisParameter Strips Bound Receivers
Strip the explicit this parameter from bound method types with OmitThisParameter so callback registries accept standalone functions without receiver errors.
Code
interface Counter {
label: string;
count: number;
}
function incrementBy(this: Counter, step: number): string {
this.count += step;
return `${this.label}:${this.count}`;
}
// OmitThisParameter drops the leading `this: Counter` constraint after .bind()
type BoundIncrement = OmitThisParameter<typeof incrementBy>;
const queue: BoundIncrement[] = [];
const apiHits: Counter = { label: 'api', count: 0 };
const boundAdd: BoundIncrement = incrementBy.bind(apiHits);
queue.push(boundAdd);
// Unbound `incrementBy` cannot enter `queue` because `this: Counter` is still required
// queue.push(incrementBy);
console.log('first call:', queue[0](2));
console.log('second call:', queue[0](3));
console.log('counter total:', apiHits.count);Output
Captured from a sandboxed run of this snippet in the build pipeline.
first call: api:2
second call: api:5
counter total: 5Inside the implementation — OmitThisParameter Strips Bound Receivers
Execution logic centers closely upon the callable incrementBy — everything else is wiring.
Points of care
- Ready to run immediately without modifying project dependencies beforehand.
Using it
Signature: function incrementBy(this: Counter, step: number)
The run prints first call:, second call:, counter total: — each prefix names the step that produced it.
Key statements
return `${this.label}:${this.count}`;
this.count += step;
Interactive Tools for This Topic
FAQ
- Are there any imports to resolve outside the language?
- No. Every import resolves against the language's own modules. The file defines `incrementBy` at load time.
Report a problem with this exampleOpens a pre-filled issue on GitHub.