Rel1cx / eslint-react

A series of composable ESLint rules for libraries and frameworks that use React as a UI runtime.
https://eslint-react.xyz
MIT License
156 stars 6 forks source link

Add TypeScript support for writing config #30

Closed hyoban closed 8 months ago

hyoban commented 8 months ago

https://github.com/eslint-types/eslint-define-config?tab=readme-ov-file#want-to-support-your-own-plugin

hyoban commented 8 months ago

I try to provide the type of rules using type calculation like below:

// eslint-define-config.d.ts
import type eslintPlugin from "./index";

type Rules = typeof eslintPlugin.rules;

declare module "eslint-define-config" {
    export type CustomRuleOptions = {
        [ruleName in keyof Rules as `@eslint-react/${ruleName}`]: Parameters<Rules[ruleName]["create"]>[0]["options"];
    };
    type A = CustomRuleOptions["@eslint-react/debug/class-component"];
    //   ^?
    type B = CustomRuleOptions["@eslint-react/naming-convention/filename"];
    //   ^?
}

The type of A is calculated correctly to empty array:

type A = []

But the type of B is miscalculated as the union of the types of the options of the two rules.

type B = readonly [{
    excepts?: readonly string[];
    rule?: "kebab-case" | "camelCase" | "PascalCase" | "snake_case";
}?] | readonly [{
    rule?: "always" | "as-needed";
}?]

The type of B should be

type B = readonly [{
    excepts?: readonly string[];
    rule?: "kebab-case" | "camelCase" | "PascalCase" | "snake_case";
}?]

I need your help @Rel1cx

Oh, I know why