I'm looking at version v2.0.0 of the state library zustand. It has an interface defined like so export interface UseStore<T extends State> { (): T <U>(selector: StateSelector<T, U>, equalityFn?: EqualityChecker<U>): U } which appears to mean (to me) that UseStore interface has two functions. The interface is used to define the first element of the return value of the create function, which is in the attached file. My question is, I don't see the two functions defined in the interface in the actual implemention. Why?
#Reading interface in relation to function
8 messages · Page 1 of 1 (latest)
It's one function with 2 overloads
Thanks, so does the interface itself defines two overloads? is that what this is export interface UseStore<T extends State> { (): T <U>(selector: StateSelector<T, U>, equalityFn?: EqualityChecker<U>): U } ex
yes
why do you call them overloads versus regular functions. Isn't it possible for an interface to have two functions?
if i heard "this interface has two functions" i'd expect the person to be talking about two different properties:
interface Foo {
one: () => string
two: () => number
}
but the UseStore type isn't like that. it's one directly-callable thing that can be called in two different ways. at runtime it's just one JS function value
For comparison, here's a more 'normal' syntax for overloads:
// public signatures
function add(x: string, y: string): string;
function add(x: number, y: number): number;
// implementation signature
function add(x: any, y: any) {
return x + y;
}
And that compiles to:
function add(x, y) { return x + y; }
there really is only one function, it's just that it has two diffeerent signatures that it can be called with