I want to implement a pattern similar to React.useState(). Basically, the function would accept one of 2 things:
Here is an example not that complex. The interface din:
interface OverloadedFn<TArgs> {
(args: TArgs): void;
(setter: (args: TArgs) => TArgs): void;
}
const overloadedFn: OverloadedFn<{ arg1: number }> = (arg) => {
if (typeof arg === 'function') {
...
} else {
...
}
}
I can write the overload, with multiple function, with type or with interface, but the args, will never get typed. I even removed the Generic. Any ideas?
TS7006: Parameter 'arg' implicitly has an 'any' type.
Here is a minimal reproducible example, in TS Playgound: https://www.typescriptlang.org/play?ts=4.1.5#code/JYOwLgpgTgZghgYwgAgPIDdoBsD2cAmE+AYiADwAqAglAOYDOAfMgN4CwAUMsgBRx30AXMmoCAlMPpgooWgG5O3HvQhhIUYXwHDRDMcgC8zXfQnIpMkPM4BfTpwQ4QU5DkxRcBIqWEZseQhJyFmR+WgBGYRAAVwBbACNoZBtmA14w00Nmdi5kYBheMABPAAcIHAKMwwM0gHIYaJAEMGAnWv0c7i7kKFVoqBBkWvDaxWTkCCwVVjHu3rB+wdqAJlHcuw4bOSA