This commit is contained in:
Andy Hanson 2017-08-30 14:30:19 -07:00
Родитель a1a74e9ead
Коммит 1369e83ff1
6 изменённых файлов: 72 добавлений и 0 удалений

27
docs/no-any-union.md Normal file
Просмотреть файл

@ -0,0 +1,27 @@
# no-any-union
Forbids to include `any` in a union. When `any` is used in a union type, the resulting type is still `any`.
**Bad**:
```ts
function f(x: string | any): void;
```
**Good**:
```ts
function f(x: string): void;
```
Or:
```ts
function f(x: any): void;
```
Or:
```ts
function f(x: string | object): void;
```
While the `string` portion of this type annotation may _look_ useful, it in fact offers no additional typechecking over simply using `any`.

Просмотреть файл

@ -1,3 +1,5 @@
# no-dead-reference
A `<reference>` comment should go at the top of a file -- otherwise it is just a normal comment.
**Bad**:

Просмотреть файл

@ -10,6 +10,7 @@
"no-padding": true,
"no-relative-import-in-test": true,
"strict-export-declare-modifiers": true,
"no-any-union": true,
"no-single-declare-module": true,
"no-useless-files": true,
"prefer-declare-function": true,

Просмотреть файл

@ -0,0 +1,32 @@
import * as Lint from "tslint";
import * as ts from "typescript";
import { failure } from "../util";
export class Rule extends Lint.Rules.AbstractRule {
static metadata: Lint.IRuleMetadata = {
ruleName: "no-any-union",
description: "Forbid a union to contain `any`",
optionsDescription: "Not configurable.",
options: null,
type: "functionality",
typescriptOnly: true,
};
static FAILURE_STRING = failure(
Rule.metadata.ruleName,
"Including `any` in a union will override all other members of the union.");
apply(sourceFile: ts.SourceFile): Lint.RuleFailure[] {
return this.applyWithFunction(sourceFile, walk);
}
}
function walk(ctx: Lint.WalkContext<void>): void {
ctx.sourceFile.forEachChild(function recur(node) {
if (node.kind === ts.SyntaxKind.AnyKeyword && ts.isUnionTypeNode(node.parent!)) {
ctx.addFailureAtNode(node, Rule.FAILURE_STRING);
}
node.forEachChild(recur);
});
}

Просмотреть файл

@ -0,0 +1,4 @@
export const x: any;
export const y: string | any;
~~~ [Including `any` in a union will override all other members of the union. See: https://github.com/Microsoft/dtslint/blob/master/docs/no-any-union.md]

Просмотреть файл

@ -0,0 +1,6 @@
{
"rulesDirectory": ["../../bin/rules"],
"rules": {
"no-any-union": true
}
}