I've encountered a scenario where I have an interface that will be utilized by multiple classes:
interface I {
readonly id: string;
process(): void;
}
My objective is to pass the id
through a constructor like so:
class A implements I {...}
let a: A = {id: "my_id"} // or new A("my_id");
I'm looking for a way to avoid repeating the default constructor implementation in each class that implements the interface, for example:
class A implements I {
constructor(id: string){ this.id = id }
}
Although I attempted to include the constructor implementation in the interface itself, I encountered a "cannot find id
" error.
Is there a solution for this in TypeScript?