UNPKG

2.52 kBMarkdownView Raw
1# `consistent-type-exports`
2
3Enforces consistent usage of type exports.
4
5TypeScript 3.8 added support for type-only exports.
6
7Type-only exports allow you to specify that 1 or more named exports are exported as type-only. This allows
8transpilers to drop exports without knowing the types of the dependencies.
9
10## Rule Details
11
12This rule aims to standardize the use of type exports style across a codebase.
13
14Given a class `Button`, and an interface `ButtonProps`, examples of code:
15
16<!--tabs-->
17
18### ❌ Incorrect
19
20```ts
21interface ButtonProps {
22 onClick: () => void;
23}
24class Button implements ButtonProps {
25 onClick() {
26 console.log('button!');
27 }
28}
29export { Button, ButtonProps };
30```
31
32### ✅ Correct
33
34```ts
35interface ButtonProps {
36 onClick: () => void;
37}
38class Button implements ButtonProps {
39 onClick() {
40 console.log('button!');
41 }
42}
43export { Button };
44export type { ButtonProps };
45```
46
47## Options
48
49```ts
50interface Options {
51 fixMixedExportsWithInlineTypeSpecifier?: boolean;
52}
53
54const defaultOptions: Options = {
55 fixMixedExportsWithInlineTypeSpecifier: false,
56};
57```
58
59### `fixMixedExportsWithInlineTypeSpecifier`
60
61When this is set to true, the rule will autofix "mixed" export cases using TS 4.5's "inline type specifier".
62If you are using a TypeScript version less than 4.5, then you will not be able to use this option.
63
64For example the following code:
65
66```ts
67const x = 1;
68type T = number;
69
70export { x, T };
71```
72
73With `{fixMixedExportsWithInlineTypeSpecifier: true}` will be fixed to:
74
75```ts
76const x = 1;
77type T = number;
78
79export { x, type T };
80```
81
82With `{fixMixedExportsWithInlineTypeSpecifier: false}` will be fixed to:
83
84```ts
85const x = 1;
86type T = number;
87
88export type { T };
89export { x };
90```
91
92<!--tabs-->
93
94### ❌ Incorrect
95
96```ts
97export { Button } from 'some-library';
98export type { ButtonProps } from 'some-library';
99```
100
101### ✅ Correct
102
103```ts
104export { Button, type ButtonProps } from 'some-library';
105```
106
107## When Not To Use It
108
109- If you are using a TypeScript version less than 3.8, then you will not be able to use this rule as type exports are not supported.
110- If you specifically want to use both export kinds for stylistic reasons, you can disable this rule.
111- If you use `--isolatedModules` the compiler would error if a type is not re-exported using `export type`. If you also don't wish to enforce one style over the other, you can disable this rule.
112
113## Attributes
114
115- Configs:
116 - [ ] ✅ Recommended
117 - [ ] 🔒 Strict
118- [x] 🔧 Fixable
119- [x] 💭 Requires type information