Can someone explain how if the below will type to a object that holds 2 functions? I can't find the docs on something like this.
interface FooContext {
bar(a: A, b: B[]): boolean;
baz(a: A, b: B[]): boolean;
}
Can someone explain how if the below will type to a object that holds 2 functions? I can't find the docs on something like this.
interface FooContext {
bar(a: A, b: B[]): boolean;
baz(a: A, b: B[]): boolean;
}
If I understood your question correctly, here are three examples of how an interface would resolve into real objects:
interface FooContext<A, B> {
bar(a: A, b: B[]): boolean;
baz(a: A, b: B[]): boolean;
}
const example1: FooContext<number, string> = {
bar(a, b) {return false},
baz(a, b) {return true}
}
class Example2 implements FooContext<string, number> {
public bar(a: string, b: number[]) {return false};
public baz(a: string, b: number[]) {return true}
}
const example2: FooContext<string, number> = new Example2();
//interface that describes a function-object
interface FunctionInterface {
(): boolean;
}
const example3: FunctionInterface = () => true;
An interface can describe any kind of object, be it an instantiated object of a class or a simple object itself. Keep in mind that arrays, functions, classes, and so on are all objects themself in javascript and can also be described by an interface.
A and B are defined in a completely different way. (You might be right though, but the OP didn't provide that info explicitly.)Your interface FooContext defines two methods: bar and baz. But it also defines the exact signature of those two methods: both the bar and baz functions take two parameters, the first one of type A and the second one of type B[], and they both return a value of type boolean.
Any object that provides at least those two methods with those signatures will comply with your FooContext interface.