Why does the compiler implicit type casting not work for literal-typed properties which are implementations of an extended interface?
Example:
// typesc
Class members are not contextually typed by the types they are declared to extend or implement, at least as of TS3.7. The extends
or implements
declarations do result in type checking of the extending/implementing class, but this happens later. This has been a pain point for a long time.
What's happening here is that literal types like 1
or 2
are widened to containing types like number
, unless they are in a non-widening context. And since myClass.num
is not contextually typed by INumContainer["num"]
, it gets widened to number
, which is seen to be incompatible.
The original issue filed about this for properties is microsoft/TypeScript#3667. There was an attempt to fix it, but it was eventually rejected/abandoned because of some poor interoperability with some real-world code libraries.
And not much has happened since. It seems that the currently open issue on this topic is microsoft/TypeScript#32082, so if you want to see this addressed you might want to go to that issue and give it a