-
-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
115 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
# Use interfaces instead of type aliases (`prefer-interface`) | ||
|
||
This rule effects failures for type alias declarations that can be declared as interfaces. | ||
|
||
<blockquote class="twitter-tweet"><p lang="en" dir="ltr">Honestly, my take is that it should really just be interfaces for anything that they can model. There is no benefit to type aliases when there are so many issues around display/perf.</p>— Daniel Rosenwasser (@drosenwasser) <a href="https://twitter.com/drosenwasser/status/1319205169918144513?ref_src=twsrc%5Etfw">October 22, 2020</a></blockquote> <script async src="https://platform.twitter.com/widgets.js" charset="utf-8"></script> | ||
|
||
## Rule details | ||
|
||
Examples of **incorrect** code for this rule: | ||
|
||
```ts | ||
type Person = { | ||
age: number; | ||
name: string; | ||
}; | ||
``` | ||
|
||
Examples of **correct** code for this rule: | ||
|
||
```ts | ||
interface Person { | ||
age: number; | ||
name: string; | ||
} | ||
``` | ||
|
||
```ts | ||
type Worker = Person | Robot; | ||
``` | ||
|
||
## Options | ||
|
||
This rule has no options. | ||
|
||
## Further reading | ||
|
||
- The [Twitter thread](https://twitter.com/robpalmer2/status/1319188885197422594) from which the above quote was taken. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
/** | ||
* @license Use of this source code is governed by an MIT-style license that | ||
* can be found in the LICENSE file at https://github.com/cartant/eslint-plugin-etc | ||
*/ | ||
|
||
import { TSESTree as es } from "@typescript-eslint/experimental-utils"; | ||
import { getParent } from "eslint-etc"; | ||
import { ruleCreator } from "../utils"; | ||
|
||
const rule = ruleCreator({ | ||
defaultOptions: [], | ||
meta: { | ||
docs: { | ||
category: "Best Practices", | ||
description: "Forbids type aliases where interfaces can be used.", | ||
recommended: false, | ||
}, | ||
fixable: undefined, | ||
messages: { | ||
forbidden: "Use an interface instead of a type alias.", | ||
}, | ||
schema: [], | ||
type: "problem", | ||
}, | ||
name: "prefer-interface", | ||
create: (context) => { | ||
return { | ||
"TSTypeAliasDeclaration > TSTypeLiteral": (node: es.Node) => { | ||
const parent = getParent(node) as es.TSTypeAliasDeclaration; | ||
context.report({ | ||
messageId: "forbidden", | ||
node: parent.id, | ||
}); | ||
}, | ||
}; | ||
}, | ||
}); | ||
|
||
export = rule; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
/** | ||
* @license Use of this source code is governed by an MIT-style license that | ||
* can be found in the LICENSE file at https://github.com/cartant/eslint-plugin-etc | ||
*/ | ||
|
||
import { stripIndent } from "common-tags"; | ||
import { fromFixture } from "eslint-etc"; | ||
import rule = require("../../source/rules/prefer-interface"); | ||
import { ruleTester } from "../utils"; | ||
|
||
ruleTester({ types: true }).run("prefer-interface", rule, { | ||
valid: [ | ||
`type T = string;`, | ||
`type T = string | number;`, | ||
`type T = "zero";`, | ||
`type T = "zero" | "one";`, | ||
`type T = { length: number; } | { width: number; };`, | ||
`type T = { length: number; } & { width: number; };`, | ||
`type T = Set<string>;`, | ||
], | ||
invalid: [ | ||
fromFixture( | ||
stripIndent` | ||
type T = { length: number; }; | ||
~ [forbidden] | ||
` | ||
), | ||
fromFixture( | ||
stripIndent` | ||
type T = { | ||
~ [forbidden] | ||
length: number; | ||
width: number; | ||
}; | ||
` | ||
), | ||
], | ||
}); |