|
2 | 2 |
|
3 | 3 | This rule enforces explicit type annotations on all function parameters, including regular parameters, destructured parameters, rest parameters, and default parameters. |
4 | 4 |
|
5 | | -## ❌ Invalid Examples |
6 | | - |
7 | | -```typescript |
8 | | -// Function declarations without parameter types |
9 | | -function processUser(user) { |
10 | | - return user.name.toUpperCase() |
11 | | -} |
12 | | - |
13 | | -function calculateSum(a, b) { |
14 | | - return a + b |
15 | | -} |
16 | | - |
17 | | -// Arrow functions without parameter types |
18 | | -const fetchData = url => { |
19 | | - return fetch(url).then(res => res.json()) |
20 | | -} |
21 | | - |
22 | | -const validateInput = (input, options) => { |
23 | | - return input.length > 0 && options.strict |
24 | | -} |
25 | | - |
26 | | -// Function expressions without parameter types |
27 | | -const handler = function (event, target) { |
28 | | - event.preventDefault() |
29 | | - target.focus() |
30 | | -} |
31 | | - |
32 | | -// Destructured parameters without types |
33 | | -function createUser({ name, email, age }) { |
34 | | - return { id: Date.now(), name, email, age } |
35 | | -} |
36 | | - |
37 | | -function processConfig({ apiUrl, timeout, retries }) { |
38 | | - return { apiUrl, timeout: timeout || 5000, retries: retries || 3 } |
39 | | -} |
40 | | - |
41 | | -// Rest parameters without types |
42 | | -function logMessages(...messages) { |
43 | | - console.log(...messages) |
44 | | -} |
45 | | - |
46 | | -function combineArrays(...arrays) { |
47 | | - return arrays.flat() |
48 | | -} |
49 | | - |
50 | | -// Default parameters without types |
51 | | -function greetUser(name = 'Guest') { |
52 | | - return `Hello, ${name}!` |
53 | | -} |
54 | | - |
55 | | -function createTimer(duration = 1000, callback) { |
56 | | - setTimeout(callback, duration) |
57 | | -} |
58 | | - |
59 | | -// Mixed parameter types without annotations |
60 | | -function complexFunction(regularParam, { destructuredProp }, ...restParams) { |
61 | | - return { regularParam, destructuredProp, restParams } |
62 | | -} |
63 | | - |
64 | | -// Class methods without parameter types |
65 | | -class UserService { |
66 | | - constructor(apiUrl, timeout) { |
67 | | - this.apiUrl = apiUrl |
68 | | - this.timeout = timeout |
69 | | - } |
| 5 | +## Examples |
| 6 | + |
| 7 | +### Basic Function with Regular Parameters |
70 | 8 |
|
71 | | - async fetchUser(id, includeProfile = false) { |
72 | | - const response = await fetch(`${this.apiUrl}/users/${id}`) |
73 | | - return response.json() |
| 9 | +```diff |
| 10 | +- function processUser(user) { |
| 11 | ++ function processUser(user: any) { |
| 12 | + return user.name.toUpperCase() |
74 | 13 | } |
| 14 | +``` |
75 | 15 |
|
76 | | - updateUser(id, { name, email }) { |
77 | | - return this.api.updateUser(id, { name, email }) |
| 16 | +### Arrow Function with Multiple Parameters |
| 17 | + |
| 18 | +```diff |
| 19 | +- const validateInput = (input, options) => { |
| 20 | ++ const validateInput = (input: any, options: any) => { |
| 21 | + return input.length > 0 && options.strict |
78 | 22 | } |
79 | | -} |
80 | 23 | ``` |
81 | 24 |
|
82 | | -## ✅ Valid Examples |
83 | | - |
84 | | -```typescript |
85 | | -// Function declarations with explicit parameter types |
86 | | -function processUser(user: { name: string; email: string }) { |
87 | | - return user.name.toUpperCase() |
88 | | -} |
89 | | - |
90 | | -function calculateSum(a: number, b: number): number { |
91 | | - return a + b |
92 | | -} |
93 | | - |
94 | | -// Arrow functions with explicit parameter types |
95 | | -const fetchData = (url: string): Promise<any> => { |
96 | | - return fetch(url).then(res => res.json()) |
97 | | -} |
98 | | - |
99 | | -const validateInput = (input: string, options: { strict: boolean }): boolean => { |
100 | | - return input.length > 0 && options.strict |
101 | | -} |
102 | | - |
103 | | -// Function expressions with explicit parameter types |
104 | | -const handler = function (event: Event, target: HTMLElement): void { |
105 | | - event.preventDefault() |
106 | | - target.focus() |
107 | | -} |
108 | | - |
109 | | -// Destructured parameters with explicit types |
110 | | -function createUser({ name, email, age }: { name: string; email: string; age: number }) { |
111 | | - return { id: Date.now(), name, email, age } |
112 | | -} |
113 | | - |
114 | | -function processConfig({ |
115 | | - apiUrl, |
116 | | - timeout, |
117 | | - retries |
118 | | -}: { |
119 | | - apiUrl: string |
120 | | - timeout?: number |
121 | | - retries?: number |
122 | | -}) { |
123 | | - return { apiUrl, timeout: timeout || 5000, retries: retries || 3 } |
124 | | -} |
125 | | - |
126 | | -// Rest parameters with explicit types |
127 | | -function logMessages(...messages: string[]): void { |
128 | | - console.log(...messages) |
129 | | -} |
130 | | - |
131 | | -function combineArrays(...arrays: any[][]): any[] { |
132 | | - return arrays.flat() |
133 | | -} |
134 | | - |
135 | | -// Default parameters with explicit types |
136 | | -function greetUser(name: string = 'Guest'): string { |
137 | | - return `Hello, ${name}!` |
138 | | -} |
139 | | - |
140 | | -function createTimer(duration: number = 1000, callback: () => void): void { |
141 | | - setTimeout(callback, duration) |
142 | | -} |
143 | | - |
144 | | -// Mixed parameter types with explicit annotations |
145 | | -function complexFunction( |
146 | | - regularParam: string, |
147 | | - { destructuredProp }: { destructuredProp: number }, |
148 | | - ...restParams: any[] |
149 | | -): object { |
150 | | - return { regularParam, destructuredProp, restParams } |
151 | | -} |
152 | | - |
153 | | -// Class methods with explicit parameter types |
154 | | -class UserService { |
155 | | - private apiUrl: string |
156 | | - private timeout: number |
157 | | - |
158 | | - constructor(apiUrl: string, timeout: number) { |
159 | | - this.apiUrl = apiUrl |
160 | | - this.timeout = timeout |
| 25 | +### Function with Destructured Parameters |
| 26 | + |
| 27 | +```diff |
| 28 | +- function createUser({ name, email, age }) { |
| 29 | ++ function createUser({ name, email, age }: any) { |
| 30 | + return { id: Date.now(), name, email, age } |
161 | 31 | } |
| 32 | +``` |
| 33 | + |
| 34 | +### Function with Rest Parameters |
162 | 35 |
|
163 | | - async fetchUser(id: string, includeProfile: boolean = false): Promise<any> { |
164 | | - const response = await fetch(`${this.apiUrl}/users/${id}`) |
165 | | - return response.json() |
| 36 | +```diff |
| 37 | +- function logMessages(...messages) { |
| 38 | ++ function logMessages(...messages: any[]) { |
| 39 | + console.log(...messages) |
166 | 40 | } |
| 41 | +``` |
| 42 | + |
| 43 | +### Function with Default Parameters |
167 | 44 |
|
168 | | - updateUser(id: string, { name, email }: { name: string; email: string }): Promise<void> { |
169 | | - return this.api.updateUser(id, { name, email }) |
| 45 | +```diff |
| 46 | +- function greetUser(name = 'Guest') { |
| 47 | ++ function greetUser(name: any = 'Guest') { |
| 48 | + return `Hello, ${name}!` |
170 | 49 | } |
171 | | -} |
172 | 50 | ``` |
173 | 51 |
|
174 | 52 | ## Rule Scope |
|
0 commit comments