Consider the code snippet below:
type NoArg = {
(): void
}
type OneArg = {
(x:number): void
}
let noArg: NoArg = (x:number)=>{}
let oneArg: OneArg = ()=>{}
The first assignment results in a compiler error, which is expected because JavaScript allows functions to be passed with fewer arguments than defined. This distinction is important when it comes to how a function is called, rather than how it's passed. More information on this topic can be found in the FAQ.
Is there a way to create a version of the OneArg interface that will not work with a zero argument function? While solutions like branding or nominal typing can achieve this, they require additional steps during assignment (such as adding the _brand property explicitly).
So, is it possible to design a type NoArg that would prevent a simple assignment like let oneArg: OneArg = ()=>{}
from succeeding?
The FAQ mentioned earlier states that "There is currently not a way in TypeScript to indicate that a callback parameter must be present." Does this completely eliminate the possibility of achieving my goal? I hope not, as this scenario involves something other than a callback parameter, but the underlying concept might be similar.
UPDATE: The comments brought up the idea of using a type guard test to accomplish this. However, it seems unlikely due to the overlap in types preventing the type guard from narrowing down the options. You can experiment with this in this playground.