Rules Hub
Coding Rules Library
Rule priority, scope & exceptions
Use this to align rules with the senior-level structure (P0/P1/P2, scope, exceptions/tradeoffs).
Forward every parameter when adapting a callback interface
A function that accepts fewer parameters than the interface declares is still assignable, so the compiler stays quiet while your adapter silently discards the arguments the caller passes.
Bad example
| 1 | interface ILogger { |
| 2 | error(message: string, ...meta: unknown[]): void; |
| 3 | } |
| 4 |
|
| 5 | // Assignable, compiles cleanly, throws away every meta argument |
| 6 | const adapter: ILogger = { |
| 7 | error: (message: string) => emitError(message), |
| 8 | }; |
Explanation (EN)
Function parameter bivariance in the arity direction means a narrower callback type-checks, so the dropped metadata is invisible until you need it during an incident.
Objašnjenje (HR)
Zbog toga sto uzi callback po broju parametara prolazi provjeru tipova, izgubljeni metapodaci nevidljivi su sve dok ti ne zatrebaju tijekom incidenta.
Good example
| 1 | const adapter: ILogger = { |
| 2 | error: (message: string, ...meta: unknown[]) => emitError(message, ...meta), |
| 3 | }; |
Explanation (EN)
The adapter mirrors the full signature and passes everything through, so the context the caller attached survives.
Objašnjenje (HR)
Adapter zrcali punu signaturu i sve prosljeduje dalje, pa kontekst koji je pozivatelj prilozio prezivljava.
Notes (EN)
Read the interface declaration rather than the call sites you happen to see. Rest parameters and trailing optional arguments are exactly what a narrowed adapter drops.
Bilješke (HR)
Procitaj deklaraciju sucelja, a ne samo pozive koje slucajno vidis. Rest parametri i zadnji neobavezni argumenti upravo su ono sto suzeni adapter odbacuje.