Some Lesser Known TypeScript Features

In the past few years, TypeScript has become a popular way to write JavaScript apps. The language is immense and can do a lot of things.

Here’s a short list of some rarer TypeScript features you may not be aware of to help with your development:

  1. You can write numbers with underscores. For example, can be written as . It will be compiled to the regular number in Javascript, but using this syntax makes things easier to read. The underscores can be placed anywhere in the number although writing is more readable than . Another good use case for this syntax is monetary amounts. may represent $25.45 in your app. This can be written as instead.
    (As mentioned by a commenter on Reddit this is also a stage 2 Javascript proposal:
  2. If you know a variable is defined you can add a after it to access it without first checking if the value is . For example, you may be injecting props into a React component using a decorator (e.g. or ). You can now write even if the definition of store is . Be careful not to overuse this feature, but there are times where it comes in handy. You can read here for further discussion of this problem. (You could always use an statement too, but that’s more verbose for cases you know the if statement is truthy).
  3. You can set a variable to have type . This means it can’t be set to anything. How is this useful? One place it can be useful is to check you’ve handled every possibility of a switch statement. For example:

The above code will throw a compile-time error, because it’s possible for to be called. If you add a switch case for with a break after it, the compile error will go away as the default block can no longer be reached.

Another potential use case for is a function containing an infinite loop or a function that always throws an exception:

Thanks to James Meyers in the comments for these use cases.

4. Another lesser used type you can use is . This just means we have no idea what the item we have is. This may happen if we’re calling a third party API for example. If we try do it will throw a compile error. We can use an variable by first doing a type check. For example, writing would no longer throw an error.

is both very similar to and the complete opposite. They’re similar in so far as with both types we don’t know what the type is. They’re the complete opposite in that will never throw a type error, but will always throw an error until we identify what it is.

Looking for some more tricks, check out this post:

Know any other useful TypeScript tricks that should be included? Feel free to mention them in the comments!

About Me

I‘m a full-stack developer and founder of Skilled. Feel free to reach out at or follow me on Twitter @elie222.


Stay up to date by subscribing to my newsletter.



Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store