Happylab Logo
Published on

Day10: Learning TypeScript - Types Unique to TS: any, unknown, void, never

Authors

Day10: Learning TypeScript - Types Unique to TS: any / unknown / void / never


any

If you do not want certain values to trigger type checking errors, you can use any, which indicates that any type is allowed.

❌ Suppose we set the type of the variable myFavoriteNumber to string, and using a number would throw an error.

let myFavoriteNumber: string = 'seven';
myFavoriteNumber = 7; // error: Cannot assign to 'myFavoriteNumber' because it is a constant.

✅ In this case, using any allows any type.

let myFavoriteNumber: any = 'seven'
myFavoriteNumber = 7
let obj: any = { x: 0 }
obj.bar = 100
obj = 'hello'
const n: number = obj
const s: string = obj
const b: boolean = obj
console.log(n) // hello

Once a variable is declared as any, any operations on it will return a value of any type. Under the any type, you can assign to any type, use any properties and methods, which is allowed. However, this is very dangerous, and any should be used sparingly unless necessary.


Undeclared Variables and Parameters are Also Considered any

If a variable is declared without specifying its type, it will be recognized as any:

Example 1:

let something
something = 7
something = 'seven'

Inference

Example 2:

function fn(s) {
  console.log(s.subtr(3))
}
fn(42)

Inference


unknown

The unknown type is similar to any, but it is safer. You cannot perform any operations on an unknown type without first asserting its type.

let value: unknown
value = 7 // ok
value = 'seven' // ok

function f1(a: unknown) {
  a.b() // error: Property 'b' does not exist on type 'unknown'.
}

function f2(a: unknown) {
  a.b() // error: Property 'b' does not exist on type 'unknown'.
}

Using unknown, it will throw an error if you try to access a property that does not exist, while any will not warn you.

Although unknown and any can accept any type of assignment, any can be assigned to any type, while unknown can only be assigned to any and itself.

let value: unknown

let value1: unknown = value // ok
let value2: any = value // ok
let value3: boolean = value // error
let value4: number = value // error
let value5: string = value // error
let value6: object = value // error
let value7: any[] = value // error
let value8: Function = value // error

void

JavaScript does not have the concept of void. In TypeScript, you can use void to indicate a function that does not return any value, as in the following example, this function only alerts and does not return any value.

function alertName(): void {
  alert('My name is iris')
}

never

The never type represents a state that should not exist, generally used for error handling functions.

function error(message: string): never {
  throw new Error(message)
}

Additionally, in the following example, when the parameter is determined to have no other types, it will also be considered never.

function fn(x: string | number) {
  if (typeof x === 'string') {
    // do something
  } else if (typeof x === 'number') {
    // do something else
  } else {
    x // has type 'never'!
  }
}

Inference


You can refer to the test examples I recorded while learning here.

In the next article, I will continue to take notes on those types unique to TypeScript. Thank you for reading, see you tomorrow!


References

https://willh.gitbook.io/typescript-tutorial/basics/any https://www.typescriptlang.org/tsconfig#noImplicitAny https://www.typescriptlang.org/docs/handbook/2/functions.html#never https://www.typescriptlang.org/docs/handbook/2/functions.html#unknown