Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(eslint-plugin): [require-types-exports] add new rule #8443

Open
wants to merge 81 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 19 commits
Commits
Show all changes
81 commits
Select commit Hold shift + click to select a range
9a0c28a
feat(eslint-plugin): [require-types-exports] add new rule
StyleShit Feb 12, 2024
7778868
wip
StyleShit Feb 12, 2024
12fce5b
wip
StyleShit Feb 12, 2024
d62f86c
lint
StyleShit Feb 13, 2024
0ebebd2
wip
StyleShit Feb 13, 2024
30e9aa9
Merge remote-tracking branch 'typescript-eslint/main' into feat/requi…
StyleShit Feb 13, 2024
bfee791
spelling...
StyleShit Feb 13, 2024
b309b51
wip
StyleShit Feb 13, 2024
6aa6446
wip
StyleShit Feb 13, 2024
892c368
wip
StyleShit Feb 13, 2024
0e8e58f
tuple generic
StyleShit Feb 13, 2024
f4018a8
wip
StyleShit Feb 13, 2024
89a8344
wip
StyleShit Feb 13, 2024
b2138e3
wip
StyleShit Feb 15, 2024
feedefd
Merge remote-tracking branch 'typescript-eslint/main' into feat/requi…
StyleShit Feb 15, 2024
1161db0
wip
StyleShit Feb 16, 2024
d9875b3
refactor
StyleShit Feb 16, 2024
6338202
make it shorter & more readable
StyleShit Feb 16, 2024
428b2c1
Merge remote-tracking branch 'typescript-eslint/main' into feat/requi…
StyleShit Feb 16, 2024
2cb3455
Merge remote-tracking branch 'typescript-eslint/main' into feat/requi…
StyleShit Apr 24, 2024
1812e37
fix nested types in functions
StyleShit Apr 24, 2024
4bee779
fix docs
StyleShit Apr 24, 2024
26e7be7
add inferred return type test case
StyleShit Apr 24, 2024
e57985a
stupidly check for variable types
StyleShit Apr 24, 2024
cbb784c
support default exported variable
StyleShit Apr 24, 2024
2f2dfa4
Merge remote-tracking branch 'typescript-eslint/main' into feat/requi…
StyleShit Apr 24, 2024
37a0171
Merge remote-tracking branch 'typescript-eslint/main' into feat/requi…
StyleShit May 19, 2024
6fb274a
update docs
StyleShit May 19, 2024
4672fe1
wip
StyleShit May 19, 2024
c79b5cb
wip
StyleShit May 19, 2024
279055a
wip
StyleShit May 19, 2024
7897abf
Merge remote-tracking branch 'typescript-eslint/main' into feat/requi…
StyleShit May 28, 2024
7082960
Merge remote-tracking branch 'typescript-eslint/main' into feat/requi…
StyleShit Jun 2, 2024
2f81933
improve types
StyleShit Jun 2, 2024
0f788d2
improve type reference search
StyleShit Jun 2, 2024
6cec0f5
don't report types from default library
StyleShit Jun 2, 2024
497957a
getTypeName
StyleShit Jun 2, 2024
702d4d0
Merge remote-tracking branch 'typescript-eslint/main' into feat/requi…
StyleShit Jun 4, 2024
700ff85
move utils out of the closure
StyleShit Jun 4, 2024
9a155b3
support namespaced types
StyleShit Jun 5, 2024
8d0d000
Merge remote-tracking branch 'typescript-eslint/main' into feat/requi…
StyleShit Jun 5, 2024
b65f9c4
fix namespaced imports
StyleShit Jun 5, 2024
078e24a
WIP
StyleShit Jun 5, 2024
ed23162
wip
StyleShit Jun 5, 2024
ac224eb
fix propertykey tests
StyleShit Jun 5, 2024
417cc91
ReturnType test
StyleShit Jun 5, 2024
62f1466
Merge remote-tracking branch 'typescript-eslint/main' into feat/requi…
StyleShit Jun 28, 2024
ae1b87c
wip
StyleShit Jun 28, 2024
d227408
collect type references recursively
StyleShit Jun 28, 2024
dca52d0
lib types
StyleShit Jun 28, 2024
ed30856
Merge remote-tracking branch 'typescript-eslint/main' into feat/requi…
StyleShit Jun 28, 2024
15fc51c
style
StyleShit Jun 28, 2024
59eda58
wip
StyleShit Jun 29, 2024
fc0858a
wip
StyleShit Jun 29, 2024
9d24c64
Merge remote-tracking branch 'typescript-eslint/main' into feat/requi…
StyleShit Jun 29, 2024
94a98eb
configs
StyleShit Jun 29, 2024
dee0fe4
don't report generic params in call expression
StyleShit Jun 29, 2024
0804b24
improve function types collection
StyleShit Jun 30, 2024
a0a4944
wip
StyleShit Jun 30, 2024
66a0aff
wip
StyleShit Jun 30, 2024
b67e1f9
remove `getVariable`
StyleShit Jun 30, 2024
9891e78
infer return type from return statements
StyleShit Jun 30, 2024
cb90d43
wip
StyleShit Jun 30, 2024
479f593
wip
StyleShit Jun 30, 2024
08f2ce2
Merge remote-tracking branch 'typescript-eslint/main' into feat/requi…
StyleShit Jun 30, 2024
e86427f
wip
StyleShit Jun 30, 2024
a61d49f
wip
StyleShit Jun 30, 2024
121f475
wip
StyleShit Jun 30, 2024
f3f8518
wip
StyleShit Jun 30, 2024
ab837b4
custom traversal
StyleShit Jul 7, 2024
1641272
some tests
StyleShit Jul 7, 2024
fd56a1c
add missing tests
StyleShit Jul 7, 2024
b0613d5
report default exported call expression
StyleShit Jul 7, 2024
b9f1148
report types used within exported types
StyleShit Jul 7, 2024
0415b60
fix false positives due to ordering
StyleShit Jul 7, 2024
a0c236e
change message
StyleShit Jul 7, 2024
3d5d695
wip
StyleShit Jul 7, 2024
2e76ce6
fix some reports
StyleShit Jul 7, 2024
88713cb
support keyof & typeof
StyleShit Jul 8, 2024
ff2c0a8
simplify tsconfig
StyleShit Jul 8, 2024
2fd30aa
Merge remote-tracking branch 'typescript-eslint/main' into feat/requi…
StyleShit Jul 8, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
75 changes: 75 additions & 0 deletions packages/eslint-plugin/docs/rules/require-types-exports.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
---
description: 'Require exporting types that are used in exported functions declarations.'
---

> 🛑 This file is source code, not the primary documentation location! 🛑
>
> See **https://typescript-eslint.io/rules/require-types-exports** for documentation.

When exporting functions from a module, it is recommended to export also all the
JoshuaKGoldberg marked this conversation as resolved.
Show resolved Hide resolved
types that are used in the function declarations. This is useful for consumers of
the module, as it allows them to use the types in their own code without having to
use things like [`Parameters`](https://www.typescriptlang.org/docs/handbook/utility-types.html#parameterstype)
StyleShit marked this conversation as resolved.
Show resolved Hide resolved
or [`ReturnType`](https://www.typescriptlang.org/docs/handbook/utility-types.html#returntypetype) to extract the types from the function.

## Examples

<!--tabs-->

### ❌ Incorrect

```ts
type Arg = string;
type Result = number;

export function strLength(arg: Arg): Result {
return arg.length;
}

interface Fruit {
name: string;
color: string;
}

export const getFruitName = (fruit: Fruit) => fruit.name;

enum Color {
Red = 'red',
Green = 'green',
Blue = 'blue',
}

export declare function getRandomColor(): Color;
```

### ✅ Correct

```ts
export type Arg = string;
export type Result = number;

export function strLength(arg: Arg): Result {
return arg.length;
}

export interface Fruit {
name: string;
color: string;
}

export const getFruitName = (fruit: Fruit) => fruit.name;

export enum Color {
Red = 'red',
Green = 'green',
Blue = 'blue',
}

export declare function getRandomColor(): Color;
```

<!--/tabs-->

## When Not To Use It

When you don't want to enforce exporting types that are used in exported functions declarations.
1 change: 1 addition & 0 deletions packages/eslint-plugin/src/configs/all.ts
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,7 @@ export = {
'@typescript-eslint/require-array-sort-compare': 'error',
'require-await': 'off',
'@typescript-eslint/require-await': 'error',
'@typescript-eslint/require-types-exports': 'error',
'@typescript-eslint/restrict-plus-operands': 'error',
'@typescript-eslint/restrict-template-expressions': 'error',
'no-return-await': 'off',
Expand Down
1 change: 1 addition & 0 deletions packages/eslint-plugin/src/configs/disable-type-checked.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ export = {
'@typescript-eslint/promise-function-async': 'off',
'@typescript-eslint/require-array-sort-compare': 'off',
'@typescript-eslint/require-await': 'off',
'@typescript-eslint/require-types-exports': 'off',
'@typescript-eslint/restrict-plus-operands': 'off',
'@typescript-eslint/restrict-template-expressions': 'off',
'@typescript-eslint/return-await': 'off',
Expand Down
1 change: 1 addition & 0 deletions packages/eslint-plugin/src/configs/strict-type-checked.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ export = {
'@typescript-eslint/prefer-ts-expect-error': 'error',
'require-await': 'off',
'@typescript-eslint/require-await': 'error',
'@typescript-eslint/require-types-exports': 'error',
'@typescript-eslint/restrict-plus-operands': 'error',
'@typescript-eslint/restrict-template-expressions': 'error',
'@typescript-eslint/triple-slash-reference': 'error',
Expand Down
2 changes: 2 additions & 0 deletions packages/eslint-plugin/src/rules/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,7 @@ import promiseFunctionAsync from './promise-function-async';
import quotes from './quotes';
import requireArraySortCompare from './require-array-sort-compare';
import requireAwait from './require-await';
import requireTypesExports from './require-types-exports';
import restrictPlusOperands from './restrict-plus-operands';
import restrictTemplateExpressions from './restrict-template-expressions';
import returnAwait from './return-await';
Expand Down Expand Up @@ -267,6 +268,7 @@ export default {
quotes: quotes,
'require-array-sort-compare': requireArraySortCompare,
'require-await': requireAwait,
'require-types-exports': requireTypesExports,
'restrict-plus-operands': restrictPlusOperands,
'restrict-template-expressions': restrictTemplateExpressions,
'return-await': returnAwait,
Expand Down
Loading
Loading