no-any-type-function

Try in Playground
typescript-best-practicesBest PracticeWarning

0

type

No CWE or CVE

Make sure we never use any as a type. TypeScript is a type-safe language and property types should be used. See our blog post for more details.

Ast Rule: function definition


no-any-type-function

How to write a rule
function visit(node, filename, code) {
  // check that we have more than one argument
  if (node.parameters && node.parameters.values && node.parameters.values.length > 0) {

    node.parameters.values.forEach(a => {
      if (a.type && a.type.astType === "string" && a.type.value === "any") {
        const error = buildError(a.type.start.line, a.type.start.col, a.type.end.line, a.type.end.col, "do not use any", "CRITICAL", "SAFETY");
        const edit = buildEditUpdate(
          a.type.start.line, a.type.start.col,
          a.type.end.line, a.type.end.col,
          "unknown");
        const fix = buildFix("use unknown instead", [edit]);

        addError(error.addFix(fix));
      }
    });

  }
}

test-any.ts

Expected test result: has error

function foo(bar: any) {
	
}

test-no-type.ts

Expected test result: no error

function foo(bar) {
	
}

test-number.ts

Expected test result: no error

function foo(bar: number) {
	
}
Add comment

Log in to add a comment


    Be the first one to leave a comment!

Codiga Logo
Codiga Hub
  • Rulesets
  • Playground
  • Snippets
  • Cookbooks
soc-2 icon

We are SOC-2 Compliance Certified

G2 high performer medal

Codiga – All rights reserved 2022.