Microsoft/TypeScript

`InstanceType` and constructor return type inference doesn’t work for built‑in subtypes of `ErrorConstructor`

Open

#61,460 建立於 2025年3月21日

在 GitHub 查看
 (4 留言) (1 反應) (0 負責人)TypeScript (6,726 fork)batch import
Domain: lib.d.tsHelp WantedPossible Improvement

倉庫指標

Star
 (48,455 star)
PR 合併指標
 (平均合併 6天 17小時) (30 天內合併 9 個 PR)

描述

🔎 Search Terms

  • "ErrorConstructor"
  • "InstanceType Error"
  • "InstanceType ErrorConstructor"

🕗 Version & Regression Information

  • This is the behaviour in every version I tried, and I reviewed the FAQ for entries about this.
  • I was unable to test this on prior versions because dtslint only tests back to TypeScript 5.0, and the bug workbench failed to download versions older than TypeScript 4.0.

⏯ Playground Link

https://www.typescriptlang.org/dev/bug-workbench/?#code/CYUwxgNghgTiAEkoGdnwMIFdkBcD2AtgKIwx4zwgAeOIAdsGiWRQN4BQAvu+zgJ4AHBADkiARngBeeAEk6uKHTAgAKoJAAeIgDcoEZuXR55OGJjD4YAPgDc7APT34z+AD0A-D35D4ogExSsiaKympCGgBKigDmIAYwRiZmFuS2Dk4uHl7qvkQAzIFyCkqq6pEgAGYgcCXxibjJlmmOLm6evDmiACyFwSVhmgDKfHQ4UFR1xg3mTXYtme3eIkQArL3FoWUDk0kzqXMZzlkdPqIAbOtj-WUAqhEyO9Mp1getx0sYRJchpeFLeBUMNh8MRSPt0m9PEA

💻 Code

declare class CustomError extends Error {
}

type NE1 = InstanceType<EvalErrorConstructor>;
//    ^?

type NE2 = InstanceType<RangeErrorConstructor>;
//    ^?

type NE3 = InstanceType<ReferenceErrorConstructor>;
//    ^?

type NE4 = InstanceType<SyntaxErrorConstructor>;
//    ^?

type NE5 = InstanceType<TypeErrorConstructor>;
//    ^?

type NE6 = InstanceType<URIErrorConstructor>;
//    ^?

type CE = InstanceType<typeof CustomError>;
//    ^?

Workbench Repro

🙁 Actual behavior

Type query results:

type NE1 = Error
type NE2 = Error
type NE3 = Error
type NE4 = Error
type NE5 = Error
type NE6 = Error
type CE = CustomError

🙂 Expected behavior

Type query results:

type NE1 = EvalError
type NE2 = RangeError
type NE3 = ReferenceError
type NE4 = SyntaxError
type NE5 = TypeError
type NE6 = URIError
type CE = CustomError

Additional information about the issue

Same happens with:

declare function newError<E extends Error>(ctor: new (msg?: string) => E): E;
declare class CustomError extends Error {
}

let ne1 = newError(EvalError);
//   ^?

let ne2 = newError(RangeError);
//   ^?

let ne3 = newError(ReferenceError);
//   ^?

let ne4 = newError(SyntaxError);
//   ^?

let ne5 = newError(TypeError);
//   ^?

let ne6 = newError(URIError);
//   ^?

let ce = newError(CustomError);
//   ^?

Workbench Repro

Related issues

貢獻者指南