Dawid Kostrzewa
Dawid Kostrzewa's Dev Stories

Dawid Kostrzewa's Dev Stories

Any vs Unknow - TypeScript use cases

Any vs Unknow - TypeScript use cases

Dawid Kostrzewa's photo
Dawid Kostrzewa
·Jan 12, 2022·

3 min read

Using TypeScript protect us from errors already on compilation stage. However there are cases when developers disable this protection by using any or unknown. Today I will try to explain how these option differ, as well as whether it is worth using them at all.

any

Using any disable type checking doing by TypeScript. We can assign values of any type to a variable.

let amount: number = 123;
let description: any;

description = 1;
description = "ABC";

amount = description;

The above code will not return any error on compilation stage. TypeScript will actually skip type checking as if we were writing in Vanilla JS. This simple example will cause runtime error when instead of number type we will use string . Much more worse will be scenario with objects when somewhere in app will be used undefined property which could potentially cause application’s crash and blank page.

We should avoid typing by any . We lose all TypeScript advantages. This is very helpful when new developers join to project and they can’t learn a lot about application when every type is any. In my projects (business and private) I always try to install eslint which protect me by using any.

unknown

unknown works similar to any. Also we can assign values of any type to a variable but here TypeScript ensure some partial type checking.

let amount: number = 123;
let description: unknown;

description = 1;
description = "ABC";

amount = description;

Code above will return compilation error

Type 'unknown' is not assignable to type 'number'.(2322)

It means that we can assign unknown type variables to other. In this case should be ensured type checking for example by simple if statement with typeof.

let amount: number = 123;
let description: unknown;

description = 1;
description = "ABC";

if(typeof description === "number") {
  amount = description;
}

any vs unknow - when to use?

It's best not to use any of them. Typing our code with any / unknown, we consciously expose ourselves to potential errors in the application's runtime. However, if we are forced to use any of them (eg we are not sure what type it will take) it is better to use uknown because unlike any it provides any minimal type checking.

Personally, I use unknown / any in several cases

  • project refactor from JS to TS - when changing the file extension from .js to .ts, variables that I am not able to quickly mark are marked as any from todo comments to correct at the earliest possible opportunity
  • When prototyping some functionalities where the current value is of little importance (except that I always mark it with the comment //TODO so as not to leave any
  • In unit tests when mocking problematic inputs

Let's try to care for the quality of the code by correct and accurate typing of variables, methods, etc., and the use of any will not only be a last resort with a comment explaining the need to use it.

 
Share this