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 46 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
46 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
File filter

Filter by extension

Filter by extension

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

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/require-types-exports** for documentation.

When exporting entities from a module, it is recommended to export also all the
types that are used in their 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 utility types like [`Parameters`](https://www.typescriptlang.org/docs/handbook/utility-types.html#parameterstype)
or [`ReturnType`](https://www.typescriptlang.org/docs/handbook/utility-types.html#returntypetype)
in order to extract the types from your code.

## Examples

<Tabs>
<TabItem value="❌ 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;
```

</TabItem>
<TabItem value="✅ 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;
```

</TabItem>
</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 @@ -143,6 +143,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 @@ -58,6 +58,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
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ export = {
'@typescript-eslint/prefer-return-this-type': 'error',
'require-await': 'off',
'@typescript-eslint/require-await': 'error',
'@typescript-eslint/require-types-exports': 'error',
'@typescript-eslint/restrict-plus-operands': [
'error',
{
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 @@ -73,6 +73,7 @@ export = {
'@typescript-eslint/prefer-return-this-type': 'error',
'require-await': 'off',
'@typescript-eslint/require-await': 'error',
'@typescript-eslint/require-types-exports': 'error',
'@typescript-eslint/restrict-plus-operands': [
'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 @@ -128,6 +128,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 @@ -274,6 +275,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