Description
π Search Terms
Variance Annotations, infer, extends, type
π Version & Regression Information
- This is the behavior in every version I tried (4.7 +), and I reviewed the FAQ for entries about "bugs-that-arent-bugs"
β― Playground Link
π» Code
interface Brand<in out T> {}
type Result = ({unrelatedField: number} & Brand<string>) extends Brand<infer T> ? T : never;
π Actual behavior
Result
is unknown
This is incorrect since the inferred type parameter is "string" not "unknown". Additionally, "unknown" is not referred to anywhere in this code, so it's very strange for it to get inferred.
π Expected behavior
Result
is string
since the type extends Brand<string>
.
This is how the example behaves if the "unrelatedField" is removed. Since that field is unrelated to the extends clause, it should have no impact on the inferred type. This is further evidence that "string" is the right result, but this case is messing that up somehow.
Additional information about the issue
Another workaround for this is to explicitly reference the type parameter in dummy fields. This is the pattern taken in my TypeCheck library to enable it to control variance before TypeScript 4.7. I found this issue when updating usages of that library to use the built-in variance annotation feature, and it broke some usages of infer.