Urara-Blog/node_modules/.pnpm-store/v3/files/ee/0faa42330e9962ad82753b5c2d212649fe6fffdc201da8f16c8163f00ec3aa5f8cd1475932f9adbd3bfbf0cb8dfe024f1889fdd9b6ec6e6ae6a27bb28f9133
2022-08-14 01:14:53 +08:00

86 lines
1.3 KiB
Text

---
description: 'Enforce type definitions to consistently use either `interface` or `type`.'
---
> 🛑 This file is source code, not the primary documentation location! 🛑
>
> See **https://typescript-eslint.io/rules/consistent-type-definitions** for documentation.
There are two ways to define a type.
```ts
// type alias
type T1 = {
a: string;
b: number;
};
// interface keyword
interface T2 {
a: string;
b: number;
}
```
## Options
This rule accepts one string option:
- `"interface"`: enforce using `interface`s for object type definitions.
- `"type"`: enforce using `type`s for object type definitions.
For example:
```jsonc
{
// Use type for object definitions
"@typescript-eslint/consistent-type-definitions": ["error", "type"]
}
```
### `interface`
Examples of code with `interface` option.
<!--tabs-->
#### ❌ Incorrect
```ts
type T = { x: number };
```
#### ✅ Correct
```ts
type T = string;
type Foo = string | {};
interface T {
x: number;
}
```
### `type`
Examples of code with `type` option.
<!--tabs-->
#### ❌ Incorrect
```ts
interface T {
x: number;
}
```
#### ✅ Correct
```ts
type T = { x: number };
```
## When Not To Use It
If you specifically want to use an interface or type literal for stylistic reasons, you can disable this rule.