TypeScript
TypeScript copied to clipboard
Infer type arguments on instance check
Fixes #17473 - Infer constrained generic parameters after instanceof check Fixes #46668 - Private field check narrows generic class too far
This is an updated and ready-to-be-reviewed recreation of my previous #49863. Please see that PR for a longer description of the problem and the proposed solution, as I don't want to duplicate it all here unnecessarily. In short, when instantiating the type of a class for an instance check, TypeScript would populate all type arguments with any no matter the constraint on the type parameter or the strictness settings. This change factors in the compiler strictness as well as the constraints and variances of the type parameters to "infer" the type arguments.
This does NOT handle #57317, per @Andarist on #57323 but I have included that test case here as these changes do introduce new behavior to that case (any to unknown). I can remove if it feels irrelevant to these changes.
Breaking Changes
From the last PR, I have resolved to use the compiler option strictInstanceOfTypeParameters to gate these otherwise breaking changes. However, there was some discussion about the possibility of not hiding these changes behind a compiler option. I can test enabling these changes for all, similar to what I did last time.