Skip to content

explicitAnys

Reports explicit uses of the any type.

✅ This rule is included in the ts logical and logicalStrict presets.

The any type in TypeScript is a dangerous “escape hatch” from the type system. Using any disables many type checking rules and is generally best used only as a last resort or when prototyping code.

When a value is typed as any:

  • Any property can be accessed without checks
  • Any method can be called without verification
  • It can be assigned to any other type
  • Type errors propagate silently through the codebase

Preferable alternatives to any include:

  • If the type is known, describing it in an interface or type
  • If the type is not known, using the safer unknown type
const
const value: any
value
: any =
const getResponse: any
getResponse
();
function
function process(input: any): void
process
(
input: any
input
: any): void {
var console: Console

The console module provides a simple debugging console that is similar to the JavaScript console mechanism provided by web browsers.

The module exports two specific components:

  • A Console class with methods such as console.log(), console.error() and console.warn() that can be used to write to any Node.js stream.
  • A global console instance configured to write to process.stdout and process.stderr. The global console can be used without importing the node:console module.

Warning: The global console object's methods are neither consistently synchronous like the browser APIs they resemble, nor are they consistently asynchronous like all other Node.js streams. See the note on process I/O for more information.

Example using the global console:

console.log('hello world');
// Prints: hello world, to stdout
console.log('hello %s', 'world');
// Prints: hello world, to stdout
console.error(new Error('Whoops, something bad happened'));
// Prints error message and stack trace to stderr:
// Error: Whoops, something bad happened
// at [eval]:5:15
// at Script.runInThisContext (node:vm:132:18)
// at Object.runInThisContext (node:vm:309:38)
// at node:internal/process/execution:77:19
// at [eval]-wrapper:6:22
// at evalScript (node:internal/process/execution:76:60)
// at node:internal/main/eval_string:23:3
const name = 'Will Robinson';
console.warn(`Danger ${name}! Danger!`);
// Prints: Danger Will Robinson! Danger!, to stderr

Example using the Console class:

const out = getStreamSomehow();
const err = getStreamSomehow();
const myConsole = new console.Console(out, err);
myConsole.log('hello world');
// Prints: hello world, to out
myConsole.log('hello %s', 'world');
// Prints: hello world, to out
myConsole.error(new Error('Whoops, something bad happened'));
// Prints: [Error: Whoops, something bad happened], to err
const name = 'Will Robinson';
myConsole.warn(`Danger ${name}! Danger!`);
// Prints: Danger Will Robinson! Danger!, to err

@seesource

console
.
Console.log(message?: any, ...optionalParams: any[]): void (+1 overload)

Prints to stdout with newline. Multiple arguments can be passed, with the first used as the primary message and all additional used as substitution values similar to printf(3) (the arguments are all passed to util.format()).

const count = 5;
console.log('count: %d', count);
// Prints: count: 5, to stdout
console.log('count:', count);
// Prints: count: 5, to stdout

See util.format() for more information.

@sincev0.1.100

log
(
input: any
input
.
any
property
);
}
function
function getData(): any
getData
(): any {
return
const fetchData: any
fetchData
();
}

This rule is not configurable.

If you do know the properties that exist on an object value, it’s generally best to use an interface or type to describe those properties. If a straightforward object type isn’t sufficient, then you can choose between several strategies instead of any. The following headings describe some of the more common strategies.

If you don’t know the data shape of a value, the unknown type is safer than any. Like any, unknown indicates the value might be any kind of data with any properties. Unlike any, unknown doesn’t allow arbitrary property accesses: it requires the value be narrowed to a more specific type before being used. See The unknown type in TypeScript for more information on unknown.

Some objects are used with arbitrary keys, especially in code that predates Maps and Sets. TypeScript interfaces may be given an “index signature” to indicate arbitrary keys are allowed on objects.

For example, this type defines an object that must have an apple property with a number value, and may have any other string keys with number | undefined values:

interface
interface AllowsAnyStrings
AllowsAnyStrings
{
AllowsAnyStrings.apple: number
apple
: number;
[
i: string
i
: string]: number | undefined;
}
let
let fruits: AllowsAnyStrings
fruits
:
interface AllowsAnyStrings
AllowsAnyStrings
;
let fruits: AllowsAnyStrings
fruits
= {
AllowsAnyStrings.apple: number
apple
: 0 }; // Ok
let fruits: AllowsAnyStrings
fruits
.
AllowsAnyStrings[string]: number | undefined
banana
= 1; // Ok
let fruits: AllowsAnyStrings
fruits
.
AllowsAnyStrings[string]: number | undefined
cherry
=
var undefined
undefined
; // Ok

See What does a TypeScript index signature actually mean? for more information on index signatures.

Some values can be one of multiple types. TypeScript allows representing these with “union” types: types that include a list of possible shapes for data.

Union types are often used to describe “nullable” values: those that can either be a data type or null and/or undefined. For example, the following StringLike type describes data that is either a string or undefined:

type
type StringLike = string | undefined
StringLike
= string | undefined;
let
let fruit: StringLike
fruit
:
type StringLike = string | undefined
StringLike
;
let fruit: StringLike
fruit
= "apple"; // Ok
let fruit: StringLike
fruit
=
var undefined
undefined
; // Ok

See TypeScript Handbook: Everyday Types > Union Types for more information on union types.

“Generic” type parameters are often used to represent a value of an unknown type. It can be tempting to use any as a type parameter constraint, but this is not recommended.

First, extends any on its own does nothing: <T extends any> is equivalent to <T>. See unnecessaryTypeConstraints for more information.

Within type parameters, never and unknown otherwise can generally be used instead. For example, the following code uses those two types in AnyFunction instead of anys to constrain Callback to any function type:

type
type AnyFunction = (...args: never[]) => unknown
AnyFunction
= (...
args: never[]
args
: never[]) => unknown;
function
function curry<Greeter extends AnyFunction>(greeter: Greeter, prefix: string): (...args: Parameters<Greeter>) => string
curry
<
function (type parameter) Greeter in curry<Greeter extends AnyFunction>(greeter: Greeter, prefix: string): (...args: Parameters<Greeter>) => string
Greeter
extends
type AnyFunction = (...args: never[]) => unknown
AnyFunction
>(
greeter: Greeter extends AnyFunction
greeter
:
function (type parameter) Greeter in curry<Greeter extends AnyFunction>(greeter: Greeter, prefix: string): (...args: Parameters<Greeter>) => string
Greeter
,
prefix: string
prefix
: string) {
return (...
args: Parameters<Greeter>
args
:
type Parameters<T extends (...args: any) => any> = T extends (...args: infer P) => any ? P : never

Obtain the parameters of a function type in a tuple

Parameters
<
function (type parameter) Greeter in curry<Greeter extends AnyFunction>(greeter: Greeter, prefix: string): (...args: Parameters<Greeter>) => string
Greeter
>) => `${
prefix: string
prefix
}: ${
greeter: Greeter
(...args: never[]) => unknown
greeter
(...
args: Parameters<Greeter>
args
)}`;
}
const
const greet: (name: string) => string
greet
= (
name: string
name
: string) => `Hello, ${
name: string
name
}!`;
const
const greetWithDate: (name: string) => string
greetWithDate
=
function curry<(name: string) => string>(greeter: (name: string) => string, prefix: string): (...args: [name: string]) => string
curry
(
const greet: (name: string) => string
greet
, "Logged: ");
const greetWithDate: (name: string) => string
greetWithDate
("linter"); // => "Logged: Hello, linter!"

See When to use never and unknown in TypeScript for more information on those types.

any is always a dangerous escape hatch. Whenever possible, it is always safer to avoid it. TypeScript’s unknown is almost always preferable to any.

However, there are occasional situations where it can be necessary to use any. Most commonly:

  • If your project isn’t fully onboarded to TypeScript yet, any can be temporarily used in places where types aren’t yet known or representable
  • If an external package doesn’t yet have typings and you want to use any pending adding a .d.ts for it
  • You’re working with particularly complex or nuanced code that can’t yet be represented in the TypeScript type system

You might consider using Flint disable comments and/or configuration file disables for those specific situations instead of completely disabling this rule.

Made with ❤️‍🔥 around the world by the Flint team and contributors.