Skip to content

Add support to define cause type, instead of always unknown #54021

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

Closed
rentalhost opened this issue Apr 26, 2023 · 3 comments
Closed

Add support to define cause type, instead of always unknown #54021

rentalhost opened this issue Apr 26, 2023 · 3 comments
Labels
Declined The issue was declined as something which matches the TypeScript vision Suggestion An idea for TypeScript

Comments

@rentalhost
Copy link

lib Update Request

Configuration Check

My compilation target is ES2022 and my lib is the default.

Missing / Incorrect Definition

I would like to suggest to improve the Error type to accept generic, so we can define an error cause directly, without workarounds on class constructors.

https://github.com/microsoft/TypeScript/blob/main/src/lib/es2022.error.d.ts

interface ErrorOptions<C = unknown> {
    cause?: C;
}

interface Error<C = unknown> {
    cause?: C;
}

interface ErrorConstructor<C = unknown> {
    new (message?: string, options?: ErrorOptions<C>): Error<C>;
    (message?: string, options?: ErrorOptions<C>): Error<C>;
}

// so on...

Sample Code

// Currently:
class CurrentlyError extends Error { }

throw new CurrentlyError("message", { cause: { code: 123 } }); // Nothing: Validates incorrectly
throw new CurrentlyError("message", { cause: { code: 'abc' } }); // Nothing: Validates incorrectly

// Workarounded:
class WorkaroundedError extends Error {
  cause: { code: number }
  constructor(message: string, cause: { cause: number }) {
    super(message, cause);
  }
}

throw new WorkaroundedError("message", { cause: { code: 123 } }); // OK: Validates correctly
throw new WorkaroundedError("message", { cause: { code: 'abc' } }); // Error: Validates correctly

// Suggestion:
class SuggestionError extends Error<{ code: number }> { }

throw new SuggestionError("message", { cause: { code: 123 } }); // OK: Validates correctly
throw new SuggestionError("message", { cause: { code: 'abc' } }); // Error: Validates correctly
@fatcerberus
Copy link

Constraining cause in this way doesn't really make sense IMO as its intended use case was meant to be

try {
    // whatever
}
catch (e) {
    throw FooError("Something stupid happened", { cause: e });
}

@rentalhost
Copy link
Author

So maybe it must be set as cause?: Error?

@RyanCavanaugh RyanCavanaugh added Suggestion An idea for TypeScript Declined The issue was declined as something which matches the TypeScript vision labels Apr 26, 2023
@RyanCavanaugh
Copy link
Member

cause is meant to wrap the inner exception, and the inner exception can be literally anything (throw null; is legal JS). So there's not really any point to try to declare this in the first place, especially with an unenforceable generic

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Declined The issue was declined as something which matches the TypeScript vision Suggestion An idea for TypeScript
Projects
None yet
Development

No branches or pull requests

3 participants