Skip to content
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

export DevalueError #49

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

angeloashmore
Copy link

This PR exports DevalueError.

Access to the error class is useful for type narrowing when access to DevalueError's path property is needed. Also helpful if other errors might be thrown in the same scope.

import * devalue from "devalue";

const foo = () => void 0;

try {
  devalue.stringify(foo);
} catch (error) {
  if (error instanceof devalue.DevalueError) {
    error.path; // typed
  }
}

I added a dumb test just to check the presence of the export. I can remove it if you think it's unnecessary.

Thanks!

@0xMurage
Copy link

What about using the object name?

try {
  devalue.stringify(foo);
} catch (err) {
  if (err.constructor.name ==="DevalueError") {
    err.path; // typed
  }
}

@angeloashmore
Copy link
Author

@0xMurage err is still unknown in your example. Checking the constructor name only checks the runtime value, not the compile-time value.

@0xMurage
Copy link

0xMurage commented Oct 24, 2024

So long as the JavaScript is executed at compile time, it should return the correct value.

@angeloashmore, could you please share a small repo that reproduces your issue?

@SukkaW
Copy link

SukkaW commented Nov 10, 2024

@angeloashmore, could you please share a small repo that reproduces your issue?

Here is a minimum re-production using TypeScript Playground.

As you can see, err is not typed:

image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants