any-type-type-var-definition

Try in Playground
typescript-best-practicesBest PracticeWarning

0

No tags

No CWE or CVE

Do not use any for variable definition. Prefer to have a clear type defined.

Ast Rule: variable declaration


any-type-type-var-definition

How to write a rule
function visit(node, filename, code) {
  if (!node.type) {
    return;
  }

  if (node.type.astType !== "string") {
    return;
  }
  if (node.type.value === "any") {
    const error = buildError(node.type.start.line, node.type.start.col,
      node.type.end.line, node.type.end.col,
      "do not use any", "CRITICAL", "SAFETY");
    const edit = buildEditUpdate(
      node.type.start.line, node.type.start.col,
      node.type.end.line, node.type.end.col,
      "unknown");
    const fix = buildFix("use unknown instead", [edit]);

    addError(error.addFix(fix));
  }

}

var-string.ts

Expected test result: no error

const foo: string = "42";

var-number.ts

Expected test result: no error

const foo: number = 42;

var-object.ts

Expected test result: has error

const foo: any = "42";
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.