Files
scriptc/tests/corpus/6957-error-interface-views.ts
T
Malte UblandClaude Opus 5.5 ab5b603e1f Keep the error itself when an interface extends Error
`interface HttpError extends Error { status: number }` receives `new Error(...)`
asserted to it. The error's fields are writable and later writes must show
through the view, so the view is the error object, not a record built from it.

A lib Error, TypeError, ... instance now carries identity into the structural
view, the same way a program class instance does.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-28 13:16:21 -07:00

43 lines
1.4 KiB
TypeScript

// An Error asserted to an interface that extends Error keeps the error
// itself: the extra fields live on the error object, and identity survives the
// conversion in both directions.
interface HttpError extends Error {
status: number | undefined;
body: string;
}
function httpError(label: string, status: number, body: string): HttpError {
const error = new Error(label + " failed") as HttpError;
error.status = status;
error.body = body;
return error;
}
const failure = httpError("GET", 500, "nope");
console.log(failure.message, failure.name, failure.status, failure.body);
console.log(failure instanceof Error);
console.log(typeof failure.stack);
function timeout(): HttpError {
const error = new Error("timed out") as HttpError;
error.name = "TimeoutError";
error.status = undefined;
error.body = "";
return error;
}
const late = timeout();
console.log(late.message, late.name, late.status, late.body === "");
// Passing the error through a function returns the same object.
function same(error: HttpError): HttpError {
return error;
}
const original = new Error("x") as HttpError;
original.status = 1;
original.body = "b";
console.log(same(original) === original, original.status, original.body);
// The error crosses out to unknown and back, still the same object.
const widened: unknown = failure;
console.log((widened as HttpError) === failure);