1 | # `consistent-type-exports`
|
2 |
|
3 | Enforces consistent usage of type exports.
|
4 |
|
5 | TypeScript 3.8 added support for type-only exports.
|
6 |
|
7 | Type-only exports allow you to specify that 1 or more named exports are exported as type-only. This allows
|
8 | transpilers to drop exports without knowing the types of the dependencies.
|
9 |
|
10 | ## Rule Details
|
11 |
|
12 | This rule aims to standardize the use of type exports style across a codebase.
|
13 |
|
14 | Given a class `Button`, and an interface `ButtonProps`, examples of code:
|
15 |
|
16 |
|
17 |
|
18 | ### ❌ Incorrect
|
19 |
|
20 | ```ts
|
21 | interface ButtonProps {
|
22 | onClick: () => void;
|
23 | }
|
24 | class Button implements ButtonProps {
|
25 | onClick() {
|
26 | console.log('button!');
|
27 | }
|
28 | }
|
29 | export { Button, ButtonProps };
|
30 | ```
|
31 |
|
32 | ### ✅ Correct
|
33 |
|
34 | ```ts
|
35 | interface ButtonProps {
|
36 | onClick: () => void;
|
37 | }
|
38 | class Button implements ButtonProps {
|
39 | onClick() {
|
40 | console.log('button!');
|
41 | }
|
42 | }
|
43 | export { Button };
|
44 | export type { ButtonProps };
|
45 | ```
|
46 |
|
47 | ## Options
|
48 |
|
49 | ```ts
|
50 | interface Options {
|
51 | fixMixedExportsWithInlineTypeSpecifier?: boolean;
|
52 | }
|
53 |
|
54 | const defaultOptions: Options = {
|
55 | fixMixedExportsWithInlineTypeSpecifier: false,
|
56 | };
|
57 | ```
|
58 |
|
59 | ### `fixMixedExportsWithInlineTypeSpecifier`
|
60 |
|
61 | When this is set to true, the rule will autofix "mixed" export cases using TS 4.5's "inline type specifier".
|
62 | If you are using a TypeScript version less than 4.5, then you will not be able to use this option.
|
63 |
|
64 | For example the following code:
|
65 |
|
66 | ```ts
|
67 | const x = 1;
|
68 | type T = number;
|
69 |
|
70 | export { x, T };
|
71 | ```
|
72 |
|
73 | With `{fixMixedExportsWithInlineTypeSpecifier: true}` will be fixed to:
|
74 |
|
75 | ```ts
|
76 | const x = 1;
|
77 | type T = number;
|
78 |
|
79 | export { x, type T };
|
80 | ```
|
81 |
|
82 | With `{fixMixedExportsWithInlineTypeSpecifier: false}` will be fixed to:
|
83 |
|
84 | ```ts
|
85 | const x = 1;
|
86 | type T = number;
|
87 |
|
88 | export type { T };
|
89 | export { x };
|
90 | ```
|
91 |
|
92 |
|
93 |
|
94 | ### ❌ Incorrect
|
95 |
|
96 | ```ts
|
97 | export { Button } from 'some-library';
|
98 | export type { ButtonProps } from 'some-library';
|
99 | ```
|
100 |
|
101 | ### ✅ Correct
|
102 |
|
103 | ```ts
|
104 | export { 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
|