6 Less Known TypeScript Types for JavaScript Developers

Daniel Pericich
5 min readSep 10, 2023

--

Photo by Mario Gogh on Unsplash

JavaScript developers starting to learn TypeScript are often surprised by how much they already seem to know. TypeScript is a superset of JavaScript meaning that all valid JavaScript is valid TypeScript. A fun thing to see when working on a new “TypeScript” project are groups of .ts files having the same code as their compiled JavaScript files.

For those looking to fully implement TypeScript in their projects, you must know several different types. Some types are familiar like strings, numbers, booleans, arrays, and objects. Along with the familiar, we also get access to custom types allowing us to combine and create unique types to fit our needs. Beyond these custom types, some types may seem unfamiliar to non-statically typing developers. Let us talk about some of the lesser-known TypeScript types.

What is any Type?

We start with one of the easiest and least useful types: any. This type can be set for parameters or variables telling TypeScript that the variable can have any value. It could be a boolean, a string, a union, or a number. What could the variable type be? Anything.

This type is mostly useless and can be prevented from being used depending on how your project’s ts.config file is set up. It is poor practice to use this type in your project. Using any is a sign of an inexperienced developer who does not know how to handle multiple possible types. My recommendation for this type is to set your “noImplicitAny” to true in tsconfig.json file and avoid this type.

What are Tuple Types?

This type is good for setting constrained lists of variables. It looks similar to an array but differs in having a preset length that will throw errors if more elements are passed. Tuples allow for mixed variable types as you define the expected type and order for each list element. Here are examples of typing a string array versus a tuple for a new employee:

Figure 1. Tuple type vs. String Array.

We set the string array and the tuple as a list of string values. However, the tuple has extra type checking as it will not allow us to set extra list elements past our defined 2. It will also throw errors if our 2 list elements are not of the correct value.

What is the never Type?

The never type is most useful when describing the output of a method. Methods can return nothing, represented by a void type output, or a value represented by a value-based type (number, boolean, string, etc.). However, if you want to show that a method will never return a value you can type the output as never.

This type is useful if a method will be returning an error, as an error will never allow a function to fully execute:

Figure 2. Type ‘never’ example

What are Union Types?

TypeScript is great for implementing type checking if you know what type you expect. What happens when you only kind of know what to expect? Say you have a module that can take input from multiple systems that pass you employee records. Some systems pass in the employee IDs as a string, while others pass in a number. You can’t force your vendors to standardize, but you want to ensure you can handle both types.

This is where union types come in. Union types allow variables to accept values of two or more types. This is the setup for a union type that accepts strings and numbers:

Figure 3. Using union type for employee id conversion.

What are Enums?

Enums are a type that makes it easy to normalize the exact values assigned to a variable. Normalizing data is fitting variable values into a set value that can be consistently stored in a table. This cuts down on the redundancy of data and helps make data easier to query against for data scientists and business analysts.

Using enums can also help limit the possible values and names used throughout your programs. For frameworks like Ruby on Rails, you may set constants to build enums for different values allowed within a model. TypeScript mirrors this by allowing enum types making it easier to normalize values and communicate intent.

With Enums, you can limit the values other developers can use when working with your code. They come in number and string versions, with number being the default value. You can read more about using each type in the TypeScript handbook.

Figure 4. Using enums to set direction values with non 0 index start.

What is the Unknown Type?

The unknown type is a supertype, much like any. Both can allow variables to have any value assigned to them, but they differ in what you can do with these variables. Any turns off any type safety allowing you to call any method on your variable. This can cause errors as you can call boolean methods on strings or string methods on numbers.

Where any allows any method call on your variables, unknown does the opposite by preventing any method calls on your variable. Even if you know that the variable is a string, you will not be able to call string methods on this variable.

Unknown can be useful if you are passing a variable from one method to another where you may need to know about the expected argument type of the second method, but don’t care about the current method’s argument type. Both unknown and any can have their valid uses, but for robust programs, you should stick to more explicit types.

--

--

Daniel Pericich
Daniel Pericich

Written by Daniel Pericich

Former Big Beer Engineer turned Full Stack Software Engineer

No responses yet