From 7897d05eca153a5c25be720b407558eb1190bb76 Mon Sep 17 00:00:00 2001 From: MorikawaSouma Date: Wed, 18 Mar 2026 12:50:50 +0800 Subject: [PATCH] fix(eslint-plugin-react-hooks): improve TypeScript types for flat configs The previous implementation initialized \ lat\ as an empty object and used Object.assign to add properties, causing TypeScript to infer the type as potentially undefined. This change: - Uses a getter function to provide proper type inference - Explicitly types the flat configs with FlatConfigs type - Removes the need for Object.assign with proper initialization - Fixes the ReactFlatConfig.plugins type to correctly reference plugin Fixes #36016 --- .../eslint-plugin-react-hooks/src/index.ts | 22 ++++++++++++++----- 1 file changed, 16 insertions(+), 6 deletions(-) diff --git a/packages/eslint-plugin-react-hooks/src/index.ts b/packages/eslint-plugin-react-hooks/src/index.ts index 924299d89894..3b26d1bb25c6 100644 --- a/packages/eslint-plugin-react-hooks/src/index.ts +++ b/packages/eslint-plugin-react-hooks/src/index.ts @@ -58,10 +58,20 @@ const recommendedLatestRuleConfigs: Linter.RulesRecord = { const plugins = ['react-hooks']; type ReactHooksFlatConfig = { - plugins: {react: any}; + plugins: {'react-hooks': typeof plugin}; rules: Linter.RulesRecord; }; +type FlatConfigs = { + recommended: ReactHooksFlatConfig; + 'recommended-latest': ReactHooksFlatConfig; +}; + +// We need to declare the type explicitly because `flat` is populated after +// `plugin` is defined (due to the circular reference: plugin.configs.flat +// needs to reference plugin itself). +let flatConfigs: FlatConfigs; + const configs = { recommended: { plugins, @@ -71,9 +81,8 @@ const configs = { plugins, rules: recommendedLatestRuleConfigs, }, - flat: {} as { - recommended: ReactHooksFlatConfig; - 'recommended-latest': ReactHooksFlatConfig; + get flat(): FlatConfigs { + return flatConfigs; }, }; @@ -86,7 +95,8 @@ const plugin = { configs, }; -Object.assign(configs.flat, { +// Initialize flat configs after plugin is defined to allow self-reference +flatConfigs = { 'recommended-latest': { plugins: {'react-hooks': plugin}, rules: configs['recommended-latest'].rules, @@ -95,6 +105,6 @@ Object.assign(configs.flat, { plugins: {'react-hooks': plugin}, rules: configs.recommended.rules, }, -}); +}; export default plugin;