fix: properly type flat config to avoid TypeScript errors#707
fix: properly type flat config to avoid TypeScript errors#707
Conversation
The flat config was typed as empty object {} which caused
TypeScript errors when users tried to access:
pluginReact.configs.flat.recommended
This fix ensures the type correctly represents the flat config structure.
Greptile SummaryThis PR fixes a TypeScript strict-mode error in Key points:
Confidence Score: 4/5
Important Files Changed
Prompt To Fix All With AIThis is a comment left during a code review.
Path: packages/eslint-plugin-react-hooks/src/index.ts
Line: 75-76
Comment:
**Double type assertion hides runtime nullability**
Using `null as unknown as ReactHooksFlatConfig` is a double type assertion (escape hatch through `unknown`) that tells TypeScript these properties are fully-initialized `ReactHooksFlatConfig` objects at all times, while their runtime value is actually `null` until the `Object.assign` call on line 89 replaces them.
In practice this is safe because `Object.assign(configs.flat, {...})` runs synchronously before the module is exported, so no consumer can ever observe the `null` values. However, `null as unknown as T` is a stronger bypass than necessary and entirely suppresses null-safety guarantees from the type system for these properties.
A slightly less aggressive alternative that avoids `null` in the object entirely would be:
```typescript
flat: {
recommended: {} as ReactHooksFlatConfig,
'recommended-latest': {} as ReactHooksFlatConfig,
},
```
This keeps a plain single-level cast (same pattern used elsewhere in the file for `basicRuleConfigs`) and avoids the runtime `null`, while still giving TypeScript concrete property keys to work with — which is the root cause of the original error.
How can I resolve this? If you propose a fix, please make it concise.Last reviewed commit: 1a99db8 |
| recommended: null as unknown as ReactHooksFlatConfig, | ||
| 'recommended-latest': null as unknown as ReactHooksFlatConfig, |
There was a problem hiding this comment.
Double type assertion hides runtime nullability
Using null as unknown as ReactHooksFlatConfig is a double type assertion (escape hatch through unknown) that tells TypeScript these properties are fully-initialized ReactHooksFlatConfig objects at all times, while their runtime value is actually null until the Object.assign call on line 89 replaces them.
In practice this is safe because Object.assign(configs.flat, {...}) runs synchronously before the module is exported, so no consumer can ever observe the null values. However, null as unknown as T is a stronger bypass than necessary and entirely suppresses null-safety guarantees from the type system for these properties.
A slightly less aggressive alternative that avoids null in the object entirely would be:
flat: {
recommended: {} as ReactHooksFlatConfig,
'recommended-latest': {} as ReactHooksFlatConfig,
},This keeps a plain single-level cast (same pattern used elsewhere in the file for basicRuleConfigs) and avoids the runtime null, while still giving TypeScript concrete property keys to work with — which is the root cause of the original error.
Prompt To Fix With AI
This is a comment left during a code review.
Path: packages/eslint-plugin-react-hooks/src/index.ts
Line: 75-76
Comment:
**Double type assertion hides runtime nullability**
Using `null as unknown as ReactHooksFlatConfig` is a double type assertion (escape hatch through `unknown`) that tells TypeScript these properties are fully-initialized `ReactHooksFlatConfig` objects at all times, while their runtime value is actually `null` until the `Object.assign` call on line 89 replaces them.
In practice this is safe because `Object.assign(configs.flat, {...})` runs synchronously before the module is exported, so no consumer can ever observe the `null` values. However, `null as unknown as T` is a stronger bypass than necessary and entirely suppresses null-safety guarantees from the type system for these properties.
A slightly less aggressive alternative that avoids `null` in the object entirely would be:
```typescript
flat: {
recommended: {} as ReactHooksFlatConfig,
'recommended-latest': {} as ReactHooksFlatConfig,
},
```
This keeps a plain single-level cast (same pattern used elsewhere in the file for `basicRuleConfigs`) and avoids the runtime `null`, while still giving TypeScript concrete property keys to work with — which is the root cause of the original error.
How can I resolve this? If you propose a fix, please make it concise.
Mirror of facebook/react#36032
Original author: SidWuChou
Bug
When using eslint-plugin-react-hooks with TypeScript strict mode, the following error occurs:
Root Cause
The flat config was typed as an empty object
{}which caused TypeScript errors when users tried to accesspluginReact.configs.flat.recommended.Fix
This fix properly initializes the flat config type to avoid TypeScript errors.
Test
The change has been tested locally with TypeScript strict mode enabled.