Question
Lets say I have two files - an interface file (like a header file in C) and an implementation file.
intf.ts (interface file):
export default interface
{
foo(baz: number): boolean
bar(baz: number): string
};
impl.ts (implementation file):
export default
{
foo(baz: number): boolean
{
return baz > 0;
}
bar(baz: number): string
{
return baz > 0 ? "john" : "doe"
}
};
How would I determine if the default export of impl.ts implements the default export intf.ts? Assume that I cannot modify either of these files.
Something like:
import intf from `./intf.ts`;
import impl from `./impl.ts`;
if (impl implements intf)
console.log("Good input!");
else
console.log("Bad input! Input does not implement interface!");
Note: The answer does not have to be determined inside a typescript script.
My partly working solution
Here is a solution I came up with that partly works:
check_if_implements.ts
import intf from `./intf.ts`;
import impl from `./impl.ts`;
function check(_: intf) {};
check(impl);
This script will create a compile-time error if the implementation does not implement the interface.
I can then compile this script and check if there is an error to determine the answer.
There is one problem with this: How do I distinguish (in code) a 'does not implement' error from any other error?
Why am I doing this?
I am trying to learn clean architecture and TypeScript works well with clean architecture because of its static type checking. For example, dependency injection is easy to do in TypeScript.
My plan is that each "module" (entity, use case, interface implementation, etc.) has an "interface" and "implementation" file (like in the question).
When modules depend on other modules, rather than depending on a name and version (like "package_name": "^1.0.2" in npm), the module depends on a certain interface. The module can copy an interface from an existing module or create their own.
The idea is that any implementation can be plugged into modules that require implementations for interfaces. I like to call this "static dependency injection", and of course "run-time dependency injection" should still be used wherever makes sense.
So this now requires me to determine if a statically injected implementation implements an interface.
Thanks for any input!