-
-
Notifications
You must be signed in to change notification settings - Fork 2.7k
/
no-throw-literal.mdx
25 lines (18 loc) · 1.21 KB
/
no-throw-literal.mdx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
---
description: 'Disallow throwing literals as exceptions.'
---
import Tabs from '@theme/Tabs';
import TabItem from '@theme/TabItem';
> 🛑 This file is source code, not the primary documentation location! 🛑
>
> See **https://typescript-eslint.io/rules/no-throw-literal** for documentation.
It is considered good practice to only `throw` the `Error` object itself or an object using the `Error` object as base objects for user-defined exceptions.
The fundamental benefit of `Error` objects is that they automatically keep track of where they were built and originated.
This rule restricts what can be thrown as an exception.
:::warning
This rule is being renamed to [`only-throw-error`](./only-throw-error.mdx).
When it was first created, it only prevented literals from being thrown (hence the name), but it has now been expanded to only allow expressions which have a possibility of being an `Error` object.
With the `allowThrowingAny` and `allowThrowingUnknown` options, it can be configured to only allow throwing values which are guaranteed to be an instance of `Error`.
The current name `no-throw-literal` will be removed in a future major version of typescript-eslint.
:::
{/* Intentionally Omitted: When Not To Use It */}