Allows declaring functions that check if a value implements a certain interface,
such that any inconsistencies are detected at compile time.
Basic issue is that typescript interfaces are compiled out when converting to
javascript. If we need a run-time check that a certain object conforms to
an interface, we need to check the presence of all the expected method names.
Implementing such checks can be extremely brittle. This method allows declaring
implementsMyInterface function that will be checked at compile time against
the target interface.
// Still have to list all required methods, but any typos will fail at compile time.
export const implementsMyInterface = makeImplementsInterfaceFunction({
someMethod: true,
someOtherMethods: true, // <-- typo; will cause compile-time error
someNotEssentialMethod: false
});
Allows declaring functions that check if a value implements a certain interface, such that any inconsistencies are detected at compile time.
Basic issue is that typescript interfaces are compiled out when converting to javascript. If we need a run-time check that a certain object conforms to an interface, we need to check the presence of all the expected method names.
Implementing such checks can be extremely brittle. This method allows declaring implementsMyInterface function that will be checked at compile time against the target interface.
Example: export interface MyInterface { someMethod(): string[]; someOtherMethod(): void; someNotEssentialMethod(): void; }
// Still have to list all required methods, but any typos will fail at compile time. export const implementsMyInterface = makeImplementsInterfaceFunction({
someMethod: true,
someOtherMethods: true, // <-- typo; will cause compile-time error
someNotEssentialMethod: false
});