|
| 1 | +<!-- This file is auto-generated by tasks/website/src/linter/rules/doc_page.rs. Do not edit it manually. --> |
| 2 | + |
| 3 | +<script setup> |
| 4 | +import { data } from '../version.data.js'; |
| 5 | +const source = `https://github.com/oxc-project/oxc/blob/${ data }/crates/oxc_linter/src/rules/typescript/explicit_module_boundary_types.rs`; |
| 6 | +</script> |
| 7 | + |
| 8 | +# typescript/explicit-module-boundary-types <Badge type="info" text="Restriction" /> |
| 9 | + |
| 10 | +<div class="rule-meta"> |
| 11 | +</div> |
| 12 | + |
| 13 | +### What it does |
| 14 | + |
| 15 | +Require explicit return and argument types on exported functions' and classes' public class methods. |
| 16 | + |
| 17 | +### Why is this bad? |
| 18 | + |
| 19 | +Explicit types for function return values and arguments makes it clear |
| 20 | +to any calling code what is the module boundary's input and output. |
| 21 | +Adding explicit type annotations for those types can help improve code |
| 22 | +readability. It can also improve TypeScript type checking performance on |
| 23 | +larger codebases. |
| 24 | + |
| 25 | +### Examples |
| 26 | + |
| 27 | +Examples of **incorrect** code for this rule: |
| 28 | + |
| 29 | +```ts |
| 30 | +// Should indicate that no value is returned (void) |
| 31 | +export function test() { |
| 32 | + return; |
| 33 | +} |
| 34 | + |
| 35 | +// Should indicate that a string is returned |
| 36 | +export var arrowFn = () => "test"; |
| 37 | + |
| 38 | +// All arguments should be typed |
| 39 | +export var arrowFn = (arg): string => `test ${arg}`; |
| 40 | +export var arrowFn = (arg: any): string => `test ${arg}`; |
| 41 | + |
| 42 | +export class Test { |
| 43 | + // Should indicate that no value is returned (void) |
| 44 | + method() { |
| 45 | + return; |
| 46 | + } |
| 47 | +} |
| 48 | +``` |
| 49 | + |
| 50 | +Examples of **correct** code for this rule: |
| 51 | + |
| 52 | +```ts |
| 53 | +// A function with no return value (void) |
| 54 | +export function test(): void { |
| 55 | + return; |
| 56 | +} |
| 57 | + |
| 58 | +// A return value of type string |
| 59 | +export var arrowFn = (): string => "test"; |
| 60 | + |
| 61 | +// All arguments should be typed |
| 62 | +export var arrowFn = (arg: string): string => `test ${arg}`; |
| 63 | +export var arrowFn = (arg: unknown): string => `test ${arg}`; |
| 64 | + |
| 65 | +export class Test { |
| 66 | + // A class method with no return value (void) |
| 67 | + method(): void { |
| 68 | + return; |
| 69 | + } |
| 70 | +} |
| 71 | + |
| 72 | +// The function does not apply because it is not an exported function. |
| 73 | +function test() { |
| 74 | + return; |
| 75 | +} |
| 76 | +``` |
| 77 | + |
| 78 | +## How to use |
| 79 | + |
| 80 | +To **enable** this rule in the CLI or using the config file, you can use: |
| 81 | + |
| 82 | +::: code-group |
| 83 | + |
| 84 | +```bash [CLI] |
| 85 | +oxlint --deny typescript/explicit-module-boundary-types |
| 86 | +``` |
| 87 | + |
| 88 | +```json [Config (.oxlintrc.json)] |
| 89 | +{ |
| 90 | + "rules": { |
| 91 | + "typescript/explicit-module-boundary-types": "error" |
| 92 | + } |
| 93 | +} |
| 94 | +``` |
| 95 | + |
| 96 | +::: |
| 97 | + |
| 98 | +## References |
| 99 | + |
| 100 | +- <a v-bind:href="source" target="_blank" rel="noreferrer">Rule Source</a> |
0 commit comments