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
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(newError('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 = newconsole.Console(out, err);
myConsole.log('hello world');
// Prints: hello world, to out
myConsole.log('hello %s', 'world');
// Prints: hello world, to out
myConsole.error(newError('Whoops, something bad happened'));
// Prints: [Error: Whoops, something bad happened], to err
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()).
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(newError('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 = newconsole.Console(out, err);
myConsole.log('hello world');
// Prints: hello world, to out
myConsole.log('hello %s', 'world');
// Prints: hello world, to out
myConsole.error(newError('Whoops, something bad happened'));
// Prints: [Error: Whoops, something bad happened], to err
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()).
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:
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:
“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: