

The difference between any and unknown type in TypeScript
source link: https://pawelgrzybek.com/the-difference-between-any-and-unknown-type-in-typescript/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

The difference between any and unknown type in TypeScript
Published: 2020.12.25 | 2 minutes read
Both any
and unknown
are universal types in TypeScript that allow you to assign whatever the heck you want to it.
let exampleAny: any;
let exampleUnknown: unknown;
// I can assign anything to `any` type
exampleAny = {}
exampleAny = 123;
// I can also assign anything to `unknown` type
exampleUnknown = {}
exampleUnknown = 123;
So you may be asking… what’s the difference between them? The difference is in accessing those values. Look!
let exampleAny: any;
let exampleUnknown: unknown;
// You can access Number.prototype method on any
// Without checking if the value is a number
console.log(exampleAny.toFixed());
// You can't access Number.prototype method on unknown
// Without checking if the value is a number
console.log(exampleUnknown.toFixed());
// ‼️ Error: Object is of type 'unknown'
Using any
disables type-checking same as @ts-ignore
does. The unknown
is a type-safe counterpart of any
type — before accessing the value, it requires type assertion or narrowing to a more specific type.
// Thats fine because before accesing Number.prototype method
// We can be assured that the type of value is a "number"
if (typeof exampleUnknown === "number") {
console.log(exampleUnknown.toFixed());
}
Rule of thumb for any and unknown type #
Use any
type only during the migration from JavaScript to TypeScript codebase, unknown
otherwise. Karma will catch you if you assign an any
type just because you can’t bothered to define meaningful type!
If you liked this article, please share it on Twitter.
Copyright © 2020 Pawel GrzybekRecommend
-
31
TypeScript 3.0 introduced a new unknown type which is the type-safe counterpart of the any type. The main difference between unknown and any is...
-
10
When implementing TypeScript in our projects, we strive to write the best typings we can. We might often feel like using the any type defeats the purpose of TypeScri...
-
10
Matt Perry|30 Aug 18Pose 3.1: Animate between any unit typeThis week we released Pose 3.1...
-
11
any, unknown and never types in Typescript This post will be a quick overview of three interesting types in Typescript: any, unknown, and never with the aim of quickly explaining what they are, and when to use them. It is part of...
-
8
TypeScript: the difference between interface and type Sign inWelcome!Log into your accountyour usernameyour password
-
6
unknown vs any in TypeScriptA variable of type any can be assigned with anything: let myVar: any = 0; myVar = '1'; myVar = false; Many TypeScript guides discourage the use of
-
11
What’s the difference between a primitive type and a class type in Java? Every variable in Java has a type, which essentially tells Java how that variable should be treated, and how much memory should be allocated for that va...
-
10
Find minimum difference between any two elementsSkip to content
-
9
Stop Using “any” Type in TypeScriptThere are better TS types and interfaces available. Why You Should Not Use “any” Type in TypeScript
-
8
Wednesday, January 31, 2024
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK