The unknown
type represents a type-safe counterpart to the any
type. It is used to indicate that the type of a value is unknown during development, providing a safer alternative to using any
. Unlike any
, variables of type unknown
cannot be assigned to other types without explicit type checking or assertion.
- 1. Declaration and Assignment:
- 2. Type Checking:
- 3. Type Assertion:
- 4. Function Parameters with Unknown:
- 5. Returning "unknown" from a Function:
- 6. Intersection with Known Types:
let userInput: unknown;
userInput = 5;
userInput = 'Hello, TypeScript!';
In this example, userInput
is declared with the unknown
type, and it can be assigned values of different types.
let userInput: unknown = 'Hello, TypeScript!';
if (typeof userInput === 'string') {
let strLength: number = userInput.length;
console.log(strLength);
}
Type checking is required before performing operations specific to a certain type. In this case, we check if userInput
is a string before getting its length.
let userInput: unknown = 'Hello, TypeScript!';
let strLength: number = (userInput as string).length;
console.log(strLength);
Type assertion (as
syntax) can be used when you are confident about the type. In this example, we assert that userInput
is a string before getting its length.
function processInput(input: unknown): void {
if (typeof input === 'string') {
console.log(input.toUpperCase());
} else {
console.log('Input is not a string');
}
}
processInput('Hello, TypeScript!');
processInput(42);
When using unknown
in function parameters, you can perform type checking inside the function based on the actual type of the argument.
function getUserData(): unknown {
// ...
}
let userData: unknown = getUserData();
Functions can return values of type unknown
when the specific type is not known until runtime.
let userInput: unknown = 'Hello, TypeScript!';
let strLength: number = (userInput as string).length; // Type assertion
let upperCaseInput: string = (userInput as string).toUpperCase(); // Type assertion
You can intersect unknown
with known types using type assertions when you are confident about the type.
Using unknown
promotes type safety by requiring explicit type checking before performing operations on values of this type. It is a more controlled alternative to using any
, making your TypeScript code safer and more maintainable.