Skip to content

Commit 1d0b42f

Browse files
committed
feat: improve types support
1 parent f2609d6 commit 1d0b42f

File tree

3 files changed

+18
-14
lines changed

3 files changed

+18
-14
lines changed

scripts/typegen.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,14 @@ const configs = await combine(
4949
vue(),
5050
)
5151

52-
const dts = await flatConfigsToRulesDTS(configs, {
52+
const configNames = configs.map(i => i.name).filter(Boolean) as string[]
53+
54+
let dts = await flatConfigsToRulesDTS(configs, {
5355
includeAugmentation: false,
5456
})
5557

58+
dts = `
59+
type ConfigNames = ${configNames.map(i => `'${i}'`).join(' | ')}
60+
${dts}`
61+
5662
await fs.writeFile('src/types/typegen.d.ts', dts)

src/index.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ import {
3838
hasVue,
3939
} from './env'
4040
import type { Awaitable, OptionsConfig, TypedFlatConfigItem } from './types'
41+
import type { Linter } from 'eslint'
4142

4243
const flatConfigProps: (keyof TypedFlatConfigItem)[] = [
4344
'name',
@@ -64,14 +65,16 @@ export const defaultPluginRenaming = {
6465
*
6566
* @param {OptionsConfig & TypedFlatConfigItem} options
6667
* The options for generating the ESLint configurations.
67-
* @param {Awaitable<TypedFlatConfigItem | TypedFlatConfigItem[]>[]} userConfigs
68+
* @param {Awaitable<TypedFlatConfigItem | TypedFlatConfigItem[] | Linter.FlatConfig[]>[]} userConfigs
6869
* The user configurations to be merged with the generated configurations.
6970
* @returns {Promise<TypedFlatConfigItem[]>}
7071
* The merged ESLint configurations.
7172
*/
7273
export async function defineConfig(
7374
options: OptionsConfig & TypedFlatConfigItem = {},
74-
...userConfigs: Awaitable<TypedFlatConfigItem | TypedFlatConfigItem[]>[]
75+
...userConfigs: Awaitable<
76+
TypedFlatConfigItem | TypedFlatConfigItem[] | Linter.FlatConfig[]
77+
>[]
7578
): Promise<TypedFlatConfigItem[]> {
7679
const {
7780
autoRenamePlugins = true,

src/types/rule.ts

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,22 @@
11
import type { Linter } from 'eslint'
2-
import type { RuleOptions } from './typegen'
2+
import type { ConfigNames, RuleOptions } from './typegen'
33
import type { BuiltInParserName, LiteralUnion, RequiredOptions } from 'prettier'
44

55
export type Rules = RuleOptions
66

7-
export type TypedFlatConfigItem = Omit<Linter.FlatConfig, 'plugins'> & {
8-
/**
9-
* Custom name of each config item
10-
*/
11-
name?: string
7+
export type { ConfigNames }
128

9+
export type TypedFlatConfigItem = Omit<
10+
Linter.FlatConfig<Linter.RulesRecord & Rules>,
11+
'plugins'
12+
> & {
1313
// Relax plugins type limitation, as most of the plugins did not have correct type info yet.
1414
/**
1515
* An object containing a name-value mapping of plugin names to plugin objects. When `files` is specified, these plugins are only available to the matching files.
1616
*
1717
* @see [Using plugins in your configuration](https://eslint.org/docs/latest/user-guide/configuring/configuration-files-new#using-plugins-in-your-configuration)
1818
*/
1919
plugins?: Record<string, any>
20-
21-
/**
22-
* An object containing a name-value mapping of rules to use.
23-
*/
24-
rules?: Linter.RulesRecord & Rules
2520
}
2621

2722
type PrettierCustomParser = 'toml'

0 commit comments

Comments
 (0)