New Best Practice
Sign In

Do not use any unless necessary

Thursday, November 7, 2019

Do not use TypeScript's any type because this removes the ability of the TypeScript compiler to perform type checking in your code. The TypeScript compiler performs in-depth type checking that can reduce typos and potential errors. When you specify the any type, either explicitly or implicitly, the compiler will no longer perform type checking on the variable or argument, and when used as the return type of a function, on any operations performed on the value returned.

It is important to also know that the default return type of a function is any where the return type cannot be inferred by the compiler.

Code Examples

/** Return type is inferred to `any`. */
function testing(valid: boolean, something: string) {
  /** The `value` variable is not immediately defined, and no type is explicitly set. */
  let value;
  if (valid) {
    value = 'test';
  } else if (something === 'else') {
    value = 0;
  }
  return value;
}

avoid: implied any type of a variable not immediately defined a value

/** The return type is inferred to the type union `string | number` */
function testing(valid: boolean, something: string) {
  let value: string | number;
  if (valid) {
    value = 'test';
  } else if (something === 'else') {
    value = 0;
  }
  return value;
}

do: explicitly set the type for variables not immediately defined a value

/** The return type is explicitly set to the type union `string | number` */
function testing(valid: boolean, something: string): string | number {
  let value: string | number;
  if (valid) {
    value = 'test';
  } else if (something === 'else') {
    value = 0;
  }
  return value;
}

do: explicitly set the type for variables as well as the return type of functions

Instructions

  • Do:

    set the expected type, or type union, of a variable or function argument when declared but no value is immediately defined

  • Avoid:

    explicitly setting the type of an argument or variable to any

  • Avoid:

    implicitly setting the type of a variable to any by not specifying the type when declaring the variable and the variable is not immediately defined

  • Avoid:

    implicitly setting the return type of a function to any by not explicitly specifying the return type, and the TypeScript compiler is unable to infer the type

  • Why:

    the any type removed the TypeScript compiler's ability to type check your code

Brian Love

Brian is a software engineer and Google Developer Expert in Web Technologies and Angular with a passion for learning, writing, speaking, teaching and mentoring. He regularly speaks at conferences and meetups around the country, and co-authored "Why Angular for the Enterprise" for O'Reilly. When not coding, Brian enjoys skiing, hiking, and being in the outdoors. Brian recently launched lookout.dev where you can find best practices and expert advice on topics ranging from TypeScript, Angular, React, Node.js and more.

Google Developers Expert

Discussions are healthy ❤️