-
Notifications
You must be signed in to change notification settings - Fork 12.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Error.prototype.cause
has inaccurate value type
#48098
Comments
Error.prototype.cause
has inaccurate valueError.prototype.cause
has inaccurate value type
The |
I'm not so sure about that - if
...without casting to |
I sort of wonder how much we pretend that non- |
As suggested to me cause could be used for giving extra info for things like typing or range errors. i.e. You might want to do something like to attach the value that caused an error: if (value === null) {
throw new TypeError("value should not be null under the current state", { cause: { value } });
}
if (n < 0) {
throw new RangeError("n should not be negative", { cause: { n } });
} |
There is also some DOM errors that doesn't extends the Error interface. |
The proposal for causes was designed very intentionally to match the type of “what can be thrown” - which is only |
sounds like a great argument for TS to teach them that their assumption is incorrect with a helpful type error when they forget to explicitly narrow. |
My two cents: @ljharb says the spec has been written very carefully so that I can tell the difference between an error with no cause and a cause which has a useless value like I say this: whatever the spec says, anyone can still put anything in |
People are having trouble with the types of my ponyfill as well: voxpelli/pony-cause#35 So all of the listed polyfills in the proposal repo are incompatible with the types in TS, that is quite telling. The type of my ponyfill: export class ErrorWithCause<T = undefined> extends Error {
constructor(message: string, { cause }?: { cause?: T; } | undefined);
cause: T;
} Preferably TS would adopt the same type as I have, or else simply go with |
The problem with this is that this very idiomatic error wrapping pattern does not compile out of the box: try {
throw new Error('hey');
} catch (err) {
throw new Error('ho', {cause: err});
} Now we have to cast: try {
throw new Error('hey');
} catch (err) {
throw new Error('ho', {cause: err as Error});
}
I understand that, but at the same time it would be great if the most idiomatic error wrapping pattern would work without too much ceremony |
See voxpelli/pony-cause#35 pony-cause is used by Umzug, so yeah, until that's resolved ES2022 is off limits.
lib Update Request
Missing / Incorrect Definition
Error.prototype.cause
's value is currently set toError
, which is inaccurate as it can accept anything, and as such should beunknown
.Sample Code
Documentation Link
https://tc39.es/ecma262/#sec-installerrorcause
The text was updated successfully, but these errors were encountered: