Closed as not planned
Description
π Search Terms
inference, generic, parameter, default
π Version & Regression Information
Behavior is the same in all version I tried.
β― Playground Link
π» Code
/* eslint-disable */
// The idea here is to propagate a primitive string union of event types
// down a class hierarchy. The weird boilerplate is to keep TS
// from automatically inferring to `string`.
export type EventInheritDefault = { type: never };
export type EventInheritType = { type: unknown };
type GetEvents<Events extends EventInheritType> = Events["type"] extends string
? Events["type"]
: never;
export type EventGetEvents<E extends EventInheritType> = GetEvents<E>;
export type InheritEvents<T extends string, E extends EventInheritType> = {
type: T | GetEvents<E>;
};
export class Base<Events extends string = string> {
emitEvent(event: Events) {}
}
export class A<
Events extends EventInheritType = EventInheritDefault
> extends Base<EventGetEvents<InheritEvents<"one" | "two", Events>>> {}
export class C<Events extends EventInheritType = EventInheritDefault> extends A<
InheritEvents<"three" | "four", Events>
> {}
const c = new C();
const base: Base = c;
c.emitEvent("three");
if (base instanceof C) {
// should be an error
base.emitEvent("thre");
// explicit cast does cause an error
const cast = base as C;
cast.emitEvent("thre");
}
π Actual behavior
The instanceof
check resolved to C<any>
instead of using the generic parameter's default.
π Expected behavior
The instanceof
check should not resolve to C<any>
and should produce an error when calling base.emitEvent("thre")
Additional information about the issue
It seems odd that an instanceof
check would produce a different result then a cast. The following code actually does work correctly:
const C2 = C as {new(): C}
if (base instanceof C2) {
// this now errors as expected
base.emitEvent('thre')
}