Type checking differs when depending on source VS compiled d.ts file for classes with generic type parameters used in private fields #36906
Labels
Duplicate
An existing issue was already created
TypeScript Version: 3.7.5 (Also confirmed in 3.5.3)
Search Terms:
unused generics
Expected behavior:
Compiling against a d.ts file produces the same type checking as checking as compiling against the original source the d.ts was built from (ex: cross package or cross project dependencies type check the same as cross module or within the file).
One way to interpret this is that for nominal typing (classes with private fields), their generic type parameters should be included in type checking, even if unused (that would fix this, but also impact other cases, though this would be an improvement for those cases as well in my opinion).
Also I expect 'no overlap' type errors to not be given between an unconstrained generic type parameter and a concreate type.
Actual behavior:
The compiled d.ts files omit the generic types from the private fields resulting in the generic type parameters being unused allowing code that should not type check to compile without error, but only when using the d.ts file instead of the source directly.
Specifically in my case, my codebase gives no type checking errors when built with projects, but gives type errors when built as a single project. I think this is a "mismatch between the checker visibility rules and the emitter output rules" (as described in #1445)
Some of the errors in my code are 'no overlap' type errors to when comparing an unconstrained generic type parameter and a concreate type: these should not be errors in the first place (I'm not sure if this is related and/or should get a separate bug), but they also shouldn't differ between the two build configurations. There were also more legitimate compile errors that were only found when doing the non-project based build that should have been detected in both builds.
As a workaround I can add dummy public fields to use the generic parameters in addition to the dummy private ones I use to ensure nominal typing. Then I can add type assertions to suppress the no overlap errors.
Related Issues:
#7797 : this issue is similar, however it is in a cause using structural typing, so the explanation for why it acts as it does does not apply to my case which uses nominal typing due to private fields. Also that issue this not really reach a conclusion.
#33877 : the example is similar, but I think it is a distinct issue.
Code
Output
Compiler Options
Playground Link: Provided
The text was updated successfully, but these errors were encountered: