Unknown and Never Type

In this lesson, we're going to study the unknown and never type in TypeScript

Project Source Code

Get the project source code below, and follow along with the lesson material.

Download Project Source Code

To set up the project on your local machine, please follow the directions provided in the README.md file. If you run into any issues with running the project source code, then feel free to reach out to the author in the course's Discord channel.

This lesson preview is part of the The newline Guide to Fullstack ASP.NET Core and React course and can be unlocked immediately with a single-time purchase. Already have access to this course? Log in here.

This video is available to students only
Unlock This Course

Get unlimited access to The newline Guide to Fullstack ASP.NET Core and React with a single-time purchase.

Thumbnail for the \newline course The newline Guide to Fullstack ASP.NET Core and React
  • [00:00 - 00:05] You are almost done with the basics of TypeScript. There is something else I want you to know.

    [00:06 - 00:16] I'm talking about the Unknown Type. Unknown Type is the type safe counterpart of the any type.

    [00:17 - 00:26] Anything is assignable to unknown, but unknown is not assignable to anything but itself and the any type. Let me give you an example.

    [00:27 - 00:45] Let's create a variable, Ola and assign type unknown to it. I'm going to create another variable, Hello, which is hello in German.

    [00:46 - 01:02] So it's H A double L O and assign a type string to it. If I say, Ola is equal to Hello, it doesn't complain.

    [01:03 - 01:19] It's acceptable because we can assign anything to type unknown, but it's not possible the other way round. If I try to assign Ola to Hello, this time other way round.

    [01:20 - 01:25] We see an error. If you hover over it, it says type unknown is not assignable to type string.

    [01:26 - 01:39] If Ola were of type any, you could assign it to any variable. Let's change it to any and you see the error is gone.

    [01:40 - 01:58] So just keep in mind that anything is assignable to unknown, but unknown is not assignable to anything but itself and type in. Likewise, no operations are permitted on an unknown without first asserting or narrowing it to a more specific type.

    [01:59 - 02:05] Let me give you an example. Let's say I want to assign the type unknown type to a string.

    [02:06 - 02:14] In this case, I'll have to narrow it down to do that. Let's change Hello to a string again.

    [02:15 - 02:38] Right now it's complaining. But if I narrow it down, let's say if type of Ola is equal to string, Hello is equal to Ola.

    [02:39 - 02:47] Now it's possible. In short, unknown type is a stricter any type, which can be only assigned to itself or any type.

    [02:48 - 02:57] Now let's talk about the never type. TypeScript has recently introduced the never type.

    [02:58 - 03:05] This basically means that values will not contain anything. The never type is used when you are sure that something is never going to happen.

    [03:06 - 03:26] For example, you write a function which will not return to its endpoint or always through an exception. Let me create a function called throwError, which accepts an error message.

    [03:27 - 03:40] Let me give it a type string. This job is to only throw an error, so new error, which is going to be the error message.

    [03:41 - 03:50] In this case, we are very sure that this function is just throwing a message and is never going to return anything. So it's safe to assign type never to it.

    [03:51 - 03:56] You must be wondering why can't we assign type void to it. Since void is the same thing.

    [03:57 - 04:04] And you're not wrong. But the point is when we use void, we can have null or undefined to be returned .

    [04:05 - 04:20] Let me create a function hello. Let me give it a type void.

    [04:21 - 04:37] Let inside the function, if I write return or return null or undefined, it won 't complain because void is allowed to return null or undefined. But the same is not correct for the never type.

    [04:38 - 04:52] If I try to return anything or null, it will show us an error. Every function is JavaScript that returns nothing, returns undefined.

    [04:53 - 05:02] But if you assign return type to be never, it doesn't even return undefined. I hope you understand what is never type and how it's different void.