-
Notifications
You must be signed in to change notification settings - Fork 13.3k
instanceof narrowing should respect type parameter bounds for covariant typesΒ #63332
Copy link
Copy link
Open
Description
π Search Terms
instanceof typeguard type parameter bounds
π Version & Regression Information
This is the behavior in every version I tried, and I reviewed the FAQ for entries about instanceof
β― Playground Link
π» Code
export class CustomError<Code extends "A" | "B"> extends Error {
readonly code: Code;
constructor(options: { code: Code, message: string }) {
super(options.message);
this.code = options.code;
}
}
declare const x: unknown;
if(x instanceof CustomError) {
x.code // inferred as any
}π Actual behavior
x.code is any, because x is only narrowed to CustomError<any>.
π Expected behavior
It would be much more useful to narrow Code in CustomError to its bounds, rather than to any. So x would become CustomError<"a" | "b"> and then x.code could usefully be "A" | "B"
Additional information about the issue
No response
Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
No labels