Unknown type vs any type in Typescript - Performance Comparison
We want to see how using unknown and any types affects the time it takes for TypeScript to check code.
How does the type checking work as code grows with these types?
Analyze the time complexity of the following TypeScript code snippet.
function processValue(value: unknown) {
if (typeof value === 'string') {
return value.toUpperCase();
} else if (typeof value === 'number') {
return value + 1;
}
return value;
}
let data: any = "hello";
let result = processValue(data);
This code checks the type of value when it is unknown and uses any to hold data without checks.
Look at what TypeScript does repeatedly when checking types.
- Primary operation: Type checking conditions on
unknownvalues. - How many times: Once per check, but grows with code size and complexity.
As code grows, TypeScript must check more conditions for unknown types, but any skips checks.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About 10 type checks for unknown, 0 for any |
| 100 | About 100 type checks for unknown, 0 for any |
| 1000 | About 1000 type checks for unknown, 0 for any |
Pattern observation: Checking unknown grows linearly with code size; any does not add checking cost.
Time Complexity: O(n)
This means the time to check types grows in a straight line as the number of unknown checks increases.
[X] Wrong: "Using any and unknown types costs the same for type checking."
[OK] Correct: any skips checks, so it is faster to check, while unknown requires TypeScript to verify types, adding time.
Understanding how different types affect checking time helps you write clearer and more efficient TypeScript code, a useful skill in real projects.
What if we replaced all unknown types with any? How would the time complexity of type checking change?
Practice
unknown and any types in TypeScript?Solution
Step 1: Understand
unknownbehaviorunknownmeans the value can be anything, but you must check its type before using it to avoid errors.Step 2: Understand
anybehavioranydisables type checking, so you can use the value without checks, which can cause runtime errors.Final Answer:
unknownrequires type checking before use,anydoes not. -> Option AQuick Check:
unknown= safe,any= unchecked [OK]
- Thinking any is safer than unknown
- Assuming unknown allows direct use without checks
- Believing unknown and any behave the same
unknown to a variable of type string?Solution
Step 1: Understand assignment rules for unknown
You cannot assignunknowndirectly to a more specific type without a type assertion or check.Step 2: Check options for correct syntax
let a: unknown = "hello"; let b: string = a as string; uses a type assertionas string, which is the correct way to tell TypeScript the value is a string.Final Answer:
let b: string = a as string; -> Option AQuick Check:
Useasto convert unknown safely [OK]
- Assigning unknown directly without assertion
- Using incorrect type assertion syntax
- Calling methods on unknown without checks
let value: unknown = 42;
if (typeof value === 'number') {
console.log(value + 8);
} else {
console.log('Not a number');
}Solution
Step 1: Check the type of
The variablevaluevalueisunknownbut assigned 42, a number.Step 2: Evaluate the if condition and output
The conditiontypeof value === 'number'is true, so it logsvalue + 8which is 42 + 8 = 50.Final Answer:
50 -> Option CQuick Check:
Type check passes, output is 50 [OK]
- Assuming unknown can be used without type check
- Expecting runtime error without type check
- Confusing unknown with any behavior
let data: unknown = 'hello'; let length: number = data.length;
Solution
Step 1: Understand property access on unknown
You cannot access properties directly on a variable typed asunknownwithout type checking or assertion.Step 2: Identify the error message
Trying to accessdata.lengthcauses a compile-time error: "Cannot access property 'length' on type 'unknown'."Final Answer:
Cannot access property 'length' on type 'unknown'. -> Option BQuick Check:
Accessing unknown properties causes error [OK]
- Assuming unknown behaves like any
- Ignoring compiler error on property access
- Wrongly typing variables
any. You want to safely use it as a string only if it really is a string. Which approach is best?Solution
Step 1: Understand risks of
anyanydisables type checking, so using it directly can cause runtime errors if the data is not as expected.Step 2: Use type guard for safety
Checking the type with a type guard (e.g.,typeof data === 'string') ensures safe usage only when the data is a string.Final Answer:
Use a type guard to check if data is a string before using it. -> Option DQuick Check:
Type guard ensures safe use of any data [OK]
- Assigning any directly without checks
- Casting any to unknown then string without validation
- Ignoring type safety for external data
Ready to prove it?
Pass the test and this pattern moves unknown
