Given the following code snippet:
class X {
bar() {
return "bar"
}
constructor(private readonly x: number){}
}
interface Y extends X {
}
const f = (y: Y) => { console.log(y.bar()); }
f({ bar: () => "tavern"});
The code does not compile due to the missing x
.
f({ bar: () => "tavern", x: 1});
Another attempt that fails to compile because x
is not private.
Trying to rewrite the code to ensure that x
can be declared as private results in this rejected solution:
class Y implements X {
bar() {
return "tavern"
}
private x = 1;
}
This approach is ineffective due to the issue of "types having separate declarations."
The only viable solution discovered so far involves removing the private
keyword from the constructor.
Ideally, the preference is for the initial solution where there's no concern about private properties or members within the constructor.
Two main questions arise:
- Why does this occur?
- Is there a way to avoid dealing with private properties altogether?