Repository navigation
[7.1 Regression] Unexpected behaviour change from Stronger Distributive Type checksΒ #64892
Description
π Search Terms
7.1 regression, TS2345
π Version & Regression Information
- This changed between versions 7.0.2 and 7.1.0-beta
β― Playground Link
π» Code
export class C {
m1<T>(
item: T,
callback: T extends string
? (item: T) => void
: (item: T) => void
): void {
callback(item);
callback(item as T extends string ? T : T);
}
m2(
item: string,
callback?: (item: string) => void
): void;
m2<T>(
item: T,
callback: (item: T) => void
): void;
m2<T>(
item: T,
callback: T extends string
? ((item: T) => void) | undefined
: (item: T) => void
): void {
if (callback) {
callback(item);
callback(item as T extends string ? T : T);
}
}
}π Actual behavior
src/main.ts:8:18 - error TS2345: Argument of type 'T' is not assignable to parameter of type 'T'.
'T' is only assignable to the non-distributed 'T', but 'T' has been distributed here.
8 callback(item);
~~~~
src/main.ts:27:22 - error TS2345: Argument of type 'T' is not assignable to parameter of type 'T'.
'T' is only assignable to the non-distributed 'T', but 'T' has been distributed here.
27 callback(item);
π Expected behavior
No Error.
Additional information about the issue
In case of m1, if tsc is aiming to strengthen Distributive Type checks, I would hope tsc can be smart enough to "undistribute" type whenever it hits some identical declarations on different branches. As for how to define identical, I suppose it may deserve further thinkings and discussions.
For m2, we have a common way of writing overload in typescript. The condition T extends string is there for determining if a method argument is optional or not. The (strengthened) disributive check that follows is kinda unintended & unwanted effect in this case. It would be weird to let the ? T : T trick to become a part of usual type overload maintenance.
I am treating the stronger checks as regression, since it actually brings undesired behaviour changes as presented above.
Activity