npm.io
0.0.1 • Published 2 years ago

catched-error-message

Licence
MIT
Version
0.0.1
Deps
0
Size
7 kB
Vulns
0
Weekly
0
Stars
1

catched-error-message

Why

Because you would like to run code as below:

try {
  throw new Error("some crazy stuff");
} catch (error) {
  doSomethingWithErrorMessage({ message: error.message });
}

But TypeScript defaults error type to unknown. And this is actually correct, since you can throw literally anything:

throw 'messgae'
throw 123
throw {but: 'why?'}
throw null
throw undefined
...

Solution

This little package is a solution for thi problem. Just use it as below:

try {
  throw new Error("some crazy stuff");
} catch (error) {
  doSomethingWithErrorMessage({ message: getErrorMessage(error) });
}

And voilà, you get your message, no TS error.

Keywords