Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: improve type suggestions with specific defineFlatConfig #150

Merged
merged 4 commits into from
Oct 30, 2022

Conversation

sxzz
Copy link
Collaborator

@sxzz sxzz commented Oct 29, 2022

  • Add unit test.
    • Types will be checked by tsc.
  • fix returning value of defineConfig.

@sxzz sxzz marked this pull request as draft October 29, 2022 18:23
@sxzz
Copy link
Collaborator Author

sxzz commented Oct 29, 2022

@Shinigami92 It's really hard to combine all kinds of configs into one function.

ignores is one of the keys of the flat config, but env is not. However, there's still a hint of env.
image

If I add env, the config will change to the normal config again.
image


It can really easily lead to a mix of the normal config and flat config. I have no idea, what do you think? @Shinigami92

@Shinigami92
Copy link
Collaborator

Try to combine all into one function:

export function defineConfig<
  Config extends ESLintConfig | FlatESLintConfig | FlatESLintConfigs // maybe with "= ESLintConfig" for default
>(config: Config): Config;

@sxzz
Copy link
Collaborator Author

sxzz commented Oct 30, 2022

@Shinigami92 I tried, but it's still like I said (#150 (comment)). All keys of config are optional, so TypeScript is unable to figure out which kind of config.

@Shinigami92
Copy link
Collaborator

Yeah, but it has two benefits

  1. the user can specify via generic argument explicitly what they want (like const conf = defineConfig<FlatESLintConfig>({})
  2. if the values of the keys are of equal type, then it doesn't matter if it's flat or non flat config

An eslint config doesn't change that often. The benefit is more like having autosuggestion for rules and see deprecated rules

@sxzz
Copy link
Collaborator Author

sxzz commented Oct 30, 2022

  • But we cannot use type generic in js files.
  • The values of flat config are not the same as config (except rules). The point is if users are not familiar with ESLint config, the hint will provide all keys of both flat config and non-flat config. It's very easy to have a mixed config with flat and non-flat config. .eslintrc is the non-flat config file, eslint.config.js is the flat config file. And then some keys will be ignored and some values could be wrong.

Shinigami92
Shinigami92 previously approved these changes Oct 30, 2022
@Shinigami92 Shinigami92 changed the title fix: returning value of define feat: improve type suggestions with specific defineFlatConfig Oct 30, 2022
@sxzz sxzz marked this pull request as ready for review October 30, 2022 14:45
@Shinigami92
Copy link
Collaborator

Yeah... okay, now I understand better what you initially mean

So I think we have no better option as defining a special defineFlatConfig so it is separated from the default eslint non flat config
Because of using LiteralUnion which results in a string, defineFlatConfig's option is poluted with functions like at() and such weird stuff 🤷
But not sure what we can do about that

@sxzz
Copy link
Collaborator Author

sxzz commented Oct 30, 2022

I think maybe we could remove LiteralUnion of PredefinedConfig, since it's a built-in config from eslint. I think it will be not changed frequently.

@Shinigami92
Copy link
Collaborator

I think maybe we could remove LiteralUnion of PredefinedConfig, since it's a built-in config from eslint. I think it will be not changed frequently.

Yes, we could do that, but doesn't it allow all kind of presets? Like plugin:@typescript-eslint/recommended

@sxzz
Copy link
Collaborator Author

sxzz commented Oct 30, 2022

@Shinigami92 No, flat config requires import plugins explicitly.

@Shinigami92 Shinigami92 merged commit c83e66c into main Oct 30, 2022
@Shinigami92 Shinigami92 deleted the fix/return-value branch October 30, 2022 14:56
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants