You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: GEMINI.md
+31-10Lines changed: 31 additions & 10 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -97,24 +97,45 @@ TypeScript's power lies in its ability to provide static type checking, catching
97
97
98
98
-**Preferring `unknown` over `any`**: When you absolutely cannot determine the type of a value at compile time, and you're tempted to reach for any, consider using unknown instead. unknown is a type-safe counterpart to any. While a variable of type unknown can hold any value, you must perform type narrowing (e.g., using typeof or instanceof checks, or a type assertion) before you can perform any operations on it. This forces you to handle the unknown type explicitly, preventing accidental runtime errors.
99
99
100
-
```
100
+
```ts
101
101
function processValue(value:unknown) {
102
-
if (typeof value === 'string') {
103
-
// value is now safely a string
104
-
console.log(value.toUpperCase());
105
-
} else if (typeof value === 'number') {
106
-
// value is now safely a number
107
-
console.log(value * 2);
108
-
}
109
-
// Without narrowing, you cannot access properties or methods on 'value'
110
-
// console.log(value.someProperty); // Error: Object is of type 'unknown'.
102
+
if (typeofvalue==='string') {
103
+
// value is now safely a string
104
+
console.log(value.toUpperCase());
105
+
} elseif (typeofvalue==='number') {
106
+
// value is now safely a number
107
+
console.log(value*2);
108
+
}
109
+
// Without narrowing, you cannot access properties or methods on 'value'
110
+
// console.log(value.someProperty); // Error: Object is of type 'unknown'.
111
111
}
112
112
```
113
113
114
114
-**Type Assertions (`as Type`) - Use with Caution**: Type assertions tell the TypeScript compiler, "Trust me, I know what I'm doing; this is definitely of this type." While there are legitimate use cases (e.g., when dealing with external libraries that don't have perfect type definitions, or when you have more information than the compiler), they should be used sparingly and with extreme caution.
115
115
-**Bypassing Type Checking**: Like `any`, type assertions bypass TypeScript's safety checks. If your assertion is incorrect, you introduce a runtime error that TypeScript would not have warned you about.
116
116
-**Code Smell in Testing**: A common scenario where `any` or type assertions might be tempting is when trying to test "private" implementation details (e.g., spying on or stubbing an unexported function within a module). This is a strong indication of a "code smell" in your testing strategy and potentially your code structure. Instead of trying to force access to private internals, consider whether those internal details should be refactored into a separate module with a well-defined public API. This makes them inherently testable without compromising encapsulation.
117
117
118
+
### Type narrowing `switch` clauses
119
+
120
+
When authoring a switch clause over an enumeration or fixed list of items,
121
+
always prefer to use the `checkExhaustive` helper method within the default
122
+
clause of the switch. This will ensure that all of the possible options within
123
+
the value or enumeration are used.
124
+
125
+
This helper method can be found in `packages/cli/src/utils/checks.ts`
126
+
127
+
Here's an example of using the helper method properly:
128
+
129
+
```
130
+
switch (someValue) {
131
+
case 1:
132
+
case 2:
133
+
// ...
134
+
default:
135
+
return checkExhaustive(someValue);
136
+
}
137
+
```
138
+
118
139
### Embracing JavaScript's Array Operators
119
140
120
141
To further enhance code cleanliness and promote safe functional programming practices, leverage JavaScript's rich set of array operators as much as possible. Methods like `.map()`, `.filter()`, `.reduce()`, `.slice()`, `.sort()`, and others are incredibly powerful for transforming and manipulating data collections in an immutable and declarative way.
0 commit comments