Skip to content
This repository has been archived by the owner on Mar 25, 2021. It is now read-only.

no-empty-interface: allow providing type arguments for extended type #3260

Merged
merged 1 commit into from
Oct 20, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 12 additions & 3 deletions src/rules/noEmptyInterfaceRule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,13 +45,22 @@ function walk(ctx: Lint.WalkContext<void>) {
return ts.forEachChild(ctx.sourceFile, function cb(node: ts.Node): void {
if (isInterfaceDeclaration(node) &&
node.members.length === 0 &&
(node.heritageClauses === undefined ||
// allow interfaces that extend 2 or more interfaces
node.heritageClauses[0].types.length < 2)) {
(node.heritageClauses === undefined || extendsOneTypeWithoutTypeArguments(node.heritageClauses[0]))) {
return ctx.addFailureAtNode(
node.name,
node.heritageClauses !== undefined ? Rule.FAILURE_STRING_FOR_EXTENDS : Rule.FAILURE_STRING);
}
return ts.forEachChild(node, cb);
});
}

function extendsOneTypeWithoutTypeArguments({types}: ts.HeritageClause): boolean {
switch (types.length) {
case 0:
return true; // don't crash on empty extends list
case 1:
return types[0].typeArguments === undefined; // allow interfaces that provide type arguments for the extended type
default:
return false; // allow interfaces extending more than one types
}
}
2 changes: 2 additions & 0 deletions test/rules/no-empty-interface/test.ts.lint
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,5 @@ interface L extends J, K {} // extending more than one interface is ok, as it ca

interface M extends {} // don't crash on empty extends list
~ [An interface declaring no members is equivalent to its supertype.]

interface N extends Promise<string|number> {}