Repository: nzakas/eslint-plugin-typescript
Pull request #13 (2016-12-13 23:14:14)

New: Add rule no-angle-bracket-type-assertion
This includes
- New rule no-angle-bracket-type-assertion
- Corresponding test
- Corresponding documentation
- Fix valid scenario in type-annotation-spacing (was failing)
- Update to README.md to include the new rule

weirdpattern: Done, I will submit a new PR for the type-annotation-spacing.

Patch:
diff --git a/README.md b/README.md
index c4c7105..2b64ffc 100644
--- a/README.md
+++ b/README.md
@@ -53,3 +53,4 @@ Then configure the rules you want to use under the rules section.
 * `typescript/explicit-member-accessibility` - enforces accessibility modifiers on class properties and methods.
 * `typescript/no-triple-slash-reference` - enforces `/// <reference />` is not used.
 * `typescript/no-explicit-any` - enforces the any type is not used.
+* `typescript/no-angle-bracket-type-assertion` - enforces the use of `as Type` assertions instead of `<Type>` assertions.
diff --git a/docs/rules/no-angle-bracket-type-assertion.md b/docs/rules/no-angle-bracket-type-assertion.md
new file mode 100644
index 0000000..3a1d568
--- /dev/null
+++ b/docs/rules/no-angle-bracket-type-assertion.md
@@ -0,0 +1,32 @@
+# Enforces the use of `as Type` assertions instead of `<Type>` assertions.
+
+TypeScript disallows the use of `<Type>` assertions in `.tsx` because of the similarity with  
+JSX's syntax, which makes it impossible to parse. 
+
+## Rule Details
+
+This rule aims to standardise the use of type assertion style across the codebase
+
+The following patterns are considered warnings:
+
+```ts
+const foo = <Foo>bar;
+```
+
+The following patterns are not warnings:
+
+```ts
+const foo = bar as Foo;
+```
+
+## When Not To Use It
+
+If your codebase does not include `.tsx` files, then you will not need this rule.
+
+## Further Reading
+
+* [Typescript and JSX](https://www.typescriptlang.org/docs/handbook/jsx.html)
+
+## Compatibility
+
+* TSLint: [no-angle-bracket-type-assertion](https://palantir.github.io/tslint/rules/no-angle-bracket-type-assertion/)
diff --git a/lib/rules/no-angle-bracket-type-assertion.js b/lib/rules/no-angle-bracket-type-assertion.js
new file mode 100644
index 0000000..988ee1e
--- /dev/null
+++ b/lib/rules/no-angle-bracket-type-assertion.js
@@ -0,0 +1,38 @@
+/**
+ * @fileoverview Enforces the use of `as Type` assertions instead of `<Type>` assertions.
+ * @author Patricio Trevino
+ */
+"use strict";
+
+//------------------------------------------------------------------------------
+// Rule Definition
+//------------------------------------------------------------------------------
+
+module.exports = {
+    meta: {
+        docs: {
+            description: "Enforces the use of as Type assertions instead of <Type> assertions.",
+            category: "Style"
+        },
+        schema: []
+    },
+
+    create: function(context) {
+
+        //----------------------------------------------------------------------
+        // Public
+        //----------------------------------------------------------------------
+        return {
+            TSTypeAssertionExpression: function(node) {
+                const type = node.typeAnnotation.typeAnnotation.typeName.name;
+                context.report({
+                    node,
+                    message: "Prefer 'as {{cast}}' instead of '<{{cast}}>' when doing type assertions",
+                    data: {
+                        cast: type
+                    }
+                });
+            }
+        };
+    }
+};


Test patch:
diff --git a/tests/lib/rules/no-angle-bracket-type-assertion.js b/tests/lib/rules/no-angle-bracket-type-assertion.js
new file mode 100644
index 0000000..3603793
--- /dev/null
+++ b/tests/lib/rules/no-angle-bracket-type-assertion.js
@@ -0,0 +1,68 @@
+/**
+ * @fileoverview Requires the use of as Type for type assertions instead of <Type>
+ * @author Patricio Trevino
+ */
+"use strict";
+
+//------------------------------------------------------------------------------
+// Requirements
+//------------------------------------------------------------------------------
+
+var rule = require("../../../lib/rules/no-angle-bracket-type-assertion"),
+    RuleTester = require("eslint").RuleTester;
+
+
+//------------------------------------------------------------------------------
+// Tests
+//------------------------------------------------------------------------------
+
+var ruleTester = new RuleTester();
+ruleTester.run("no-angle-bracket-type-assertion", rule, {
+
+    valid: [
+        {
+            code: `
+interface Foo { 
+    bar : number;
+    bas : string;
+}
+
+class Generic<T> implements Foo {}
+
+const foo = {} as Foo<int>;
+const bar = new Generic<int>() as Foo;
+const array : Array<string> = [];
+            `,
+            parser: "typescript-eslint-parser"
+        }
+    ],
+    invalid: [
+        {
+            code: `
+interface Foo { 
+    bar : number;
+    bas : string;
+}
+
+class Generic<T> implements Foo {}
+
+const foo = <Foo>{};
+const bar = <Foo>new Generic<int>();
+const array : Array<string> = [];
+            `,
+            parser: "typescript-eslint-parser",
+            errors: [
+                {
+                    message: "Prefer 'as Foo' instead of '<Foo>' when doing type assertions",
+                    row: 8,
+                    column: 13
+                },
+                {
+                    message: "Prefer 'as Foo' instead of '<Foo>' when doing type assertions",
+                    row: 9,
+                    column: 13
+                },
+            ]
+        }
+    ]
+});