Closed as not planned
Description
π Search Terms
instanceof, narrowing, prototype
π Version & Regression Information
- This changed between versions 5.3.3 and 5.4.0-beta.
- This changed on 5.4.0-dev.20231130 (commits)
β― Playground Link
π» Code
(reduced down from large repo as best I could)
declare class Widget {
destroy(): void;
}
declare const WidgetConstructor: {
new(): Widget;
}
declare const MenuWidgetClass: {
new(): {
isMenuList: boolean;
} & Widget;
prototype: typeof WidgetConstructor; // <-- removing this avoids error
};
type MenuWidget = InstanceType<typeof MenuWidgetClass>;
declare const MenuWidget: typeof MenuWidgetClass & {
new(): MenuWidget
};
/////////////////////////////////////////////////////////
let content: MenuWidget | undefined;
declare const v: Widget;
if (v instanceof MenuWidget) {
content = v; // <-- new error in 5.4.0-beta
}
New behavior
TypeScript 5.4.0-beta
appears to be consulting the .prototype
property of the type in a way that it wasn't before resulting in the instanceof
narrowing to narrow to the base class.
Previous behavior
The code in 5.3.3
was expecting the instanceof
to narrow to the return type of the function being consulted.
Additional information about the issue
Note: this is more of a change report than a bug report, as I don't think this was announced in the 5.4.0-beta breaking changes. I suspect the change is an improvement and is catching a potential issue that was previously unsafe.