Skip to content

Commit aaebbd9

Browse files
committed
fix: Include signature type parameters, too.
1 parent 4e928fa commit aaebbd9

File tree

2 files changed

+19
-6
lines changed

2 files changed

+19
-6
lines changed

source/rules/prefer-interface.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,9 +57,12 @@ const rule = ruleCreator({
5757
return;
5858
}
5959
function fix(fixer: eslint.RuleFixer) {
60-
const typeParameters = typeAliasNode.typeParameters
60+
const interfaceTypeParameters = typeAliasNode.typeParameters
6161
? context.getSourceCode().getText(typeAliasNode.typeParameters)
6262
: "";
63+
const functionTypeParameters = functionTypeNode.typeParameters
64+
? context.getSourceCode().getText(functionTypeNode.typeParameters)
65+
: "";
6366
const params = functionTypeNode.params
6467
.map((param) => context.getSourceCode().getText(param))
6568
.join(",");
@@ -71,7 +74,7 @@ const rule = ruleCreator({
7174
: "void";
7275
return fixer.replaceText(
7376
typeAliasNode,
74-
`interface ${typeAliasNode.id.name}${typeParameters} { (${params}): ${returnType}; }`
77+
`interface ${typeAliasNode.id.name}${interfaceTypeParameters} { ${functionTypeParameters}(${params}): ${returnType}; }`
7578
);
7679
}
7780
context.report({

tests/rules/prefer-interface.ts

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -132,13 +132,23 @@ ruleTester({ types: true }).run("prefer-interface", rule, {
132132
),
133133
fromFixture(
134134
stripIndent`
135-
export type Identity<T> = (value: T) => T;
136-
~~~~~~~~ [forbidden]
135+
type Identity<T> = (value: T) => T;
136+
~~~~~~~~ [forbidden]
137+
`,
138+
{
139+
output: stripIndent`
140+
interface Identity<T> { (value: T): T; }
141+
`,
142+
}
143+
),
144+
fromFixture(
145+
stripIndent`
146+
type Func<Foo> = <Bar>(foo: Foo) => Bar;
147+
~~~~ [forbidden]
137148
`,
138149
{
139-
options: [{ allowLocal: true }],
140150
output: stripIndent`
141-
export interface Identity<T> { (value: T): T; }
151+
interface Func<Foo> { <Bar>(foo: Foo): Bar; }
142152
`,
143153
}
144154
),

0 commit comments

Comments
 (0)