Editor’s note: This article was last updated by Ikeh Akinyemi on 24 June 2024 to briefly cover the differences between type assertion and type casting, methods for transforming data types such as casting strings to numbers and values to strings, and more.
TypeScript introduces a robust type system that enables developers to define and enforce types for variables, function parameters, return values, and more. TypeScript’s type system provides static type checking, allowing you to identify and prevent potential errors before runtime.
Type casting is a feature in TypeScript that allows developers to explicitly change the type of a value from one type to another. Type casting is particularly useful when you’re working with dynamic data, or when the type of a value is not correctly inferred automatically.
In this article, we’ll explore the ins and outs of type casting in TypeScript. To follow along, you’ll need working knowledge of TypeScript and object-oriented programming.
Type casting can happen in one of two ways: it can be implicit, which is when TypeScript handles the operation, or explicit, when the developer handles the conversion. Implicit casting occurs when TypeScript sees a type error and attempts to safely correct it.
Type casting is essential for performing various operations, including mathematic calculations, and data, manipulation, and compatibility checks. But before you can start using type casting effectively, you’ll need to understand some foundational concepts like subtype and supertype relationships, type widening, and type narrowing.
While these two terms are often used interchangeably amongst developers, there is a subtle difference between type assertion and type casting in TypeScript:
as
keyword, which we explored aboveString()
, Number()
, Boolean()
, etc.The key difference is that type assertion is purely a compile-time construct — it tells TypeScript to treat a value as a certain type without affecting its runtime behavior. Type casting, on the other hand, actually transforms the data and can affect runtime behavior.
One way to classify types is to split them into sub- and supertypes. Generally, a subtype is a specialized version of a supertype that inherits the supertype’s attributes and behaviors. A supertype, on the other hand, is a more general type that is the basis of multiple subtypes.
Consider a scenario where you have a class hierarchy with a superclass called Animal
and two subclasses named Cat
and Dog
. Here, Animal
is the supertype, while Cat
and Dog
are the subtypes. Type casting comes in handy when you need to treat an object of a particular subtype as its supertype or vice versa.
Type widening, or upcasting, occurs when you need to convert a variable from a subtype to a supertype. Type widening is usually implicit, meaning that it is performed by TypeScript, because it involves moving from a narrow category to a broader one. Type widening is safe and it won’t cause any errors because a subtype inherently possesses all the attributes and behaviors of its supertype.
Type narrowing, or downcasting, occurs when you convert a variable from a supertype to a subtype. Type narrowing conversion is explicit and requires a type assertion or a type check to ensure the validity of the conversion. This process can be risky because not all supertype variables hold values that are compatible with the subtype.
as
operatorThe as
operator is TypeScript’s primary mechanism for explicit type casting. With its intuitive syntax, as
allows you to inform the compiler about the intended type of a variable or expression.
Below is the general form of the as
operator:
value as Type
Here, value
represents the variable or expression you can cast, while Type
denotes the desired target type. By using as
, you explicitly assert that value
is of type Type
.
The as
operator is useful when you’re working with types that have a common ancestor, including class hierarchies or interface implementations. It allows you to indicate that a particular variable should be treated as a more specific subtype. Here’s some code to illustrate:
class Animal { eat(): void { console.log('Eating...'); } } class Dog extends Animal { bark(): void { console.log('Woof!'); } } const animal: Animal = new Dog(); const dog = animal as Dog; dog.bark(); // Output: "Woof!"
In this code, the Dog
class extends the Animal
class. The Dog
instance is assigned to a variable animal
of type Animal
. By using the as
operator, you cast animal
as Dog
, allowing you to access the bark()
method specific to the Dog
class. The code should output this:
You can use the as
operator to cast to specific types. This capability comes in handy when you need to interact with a type that differs from the one inferred by TypeScript’s type inference system. Here’s an example:
function getLength(obj: any): number { if (typeof obj === 'string') { return (obj as string).length; } else if (Array.isArray(obj)) { return (obj as any[]).length; } return 0; }
The getLength
function accepts a parameter obj
of type any
. In the getLength
function, the as
operator casts obj
to a string for any[]
based on its type. This operation gives you access to the length
property specific to strings or arrays, respectively.
Additionally, you can cast to a Union type to express that a value can be one of several types:
function processValue(value: string | number): void { if (typeof value === 'string') { console.log((value as string).toUpperCase()); } else { console.log((value as number).toFixed(2)); } }
The processValue
function accepts a parameter value
of type string | number
, indicating that it can be a string or a number. By using the as
operator, you cast value
to string
or number
within the respective conditions, allowing you to apply type-specific operations such as toUpperCase()
or toFixed()
.
as
operatorWhile the as
operator is a powerful tool for type casting in TypeScript, it has some limitations. One limitation is that as
operates purely at compile-time and does not perform any runtime checks. This means that if the casted type is incorrect, it may result in runtime errors. So, it is crucial to ensure the correctness of the type being cast.
Another limitation of the as
operator is that you can’t use it to cast between unrelated types. TypeScript’s type system provides strict checks to prevent unsafe casting, ensuring type safety throughout your codebase. In such cases, consider alternative approaches, such as type assertion functions or type guards.
as
castingThere are instances when TypeScript raises objections and refuses to grant permission for as
casting. Let’s look at some situations that might cause this.
TypeScript’s static type checking relies heavily on the structural compatibility of types, including custom types. When you try to cast a value with the as
operator, the compiler assesses the structural compatibility between the original type and the desired type.
If the structural properties of the two custom types are incompatible, TypeScript will raise an error, signaling that the casting operation is unsafe. Here’s an example of type casting with structural incompatibility errors using custom types:
interface Square { sideLength: number; } interface Rectangle { width: number; height: number; } const square: Square = { sideLength: 5 }; const rectangle = square as Rectangle; // Error: Incompatible types
TypeScript prevents the as
casting operation because the two custom types, Square
and Rectangle
, have different structural properties. Instead of relying on the as
operator casting, a safer approach would be to create a new instance of the desired type, and then manually assign the corresponding values.
Union types in TypeScript allow you to define a value that can be one of several possible types. Type guards play a crucial role in narrowing down the specific type of a value within a conditional block, enabling type-safe operations.
However, when attempting to cast a union type with the as
operator, it is required that the desired type be one of the constituent types of the union. If the desired type is not included in the union, TypeScript won’t allow the casting operation:
type Shape = Square | Rectangle; function getArea(shape: Shape) { if ('sideLength' in shape) { // Type guard: 'sideLength' property exists, so shape is of type Square return shape.sideLength ** 2; } else { // shape is of type Rectangle return shape.width * shape.height; } } const square: Shape = { sideLength: 5 }; const area = getArea(square); // Returns 25
In the above snippet, you have a union type Shape
that represents either a Square
or Rectangle
. The getArea
function takes a parameter of type Shape
and needs to calculate the area based on the specific shape.
To determine the type of shape
inside the getArea
function, we use a type guard. The type guard checks for the presence of the sideLength
property using the in
operator. If the sideLength
property exists, TypeScript narrows down the type of shape
to Square
within that conditional block, allowing us to access the sideLength
property safely.
Type assertions, denoted with the as
keyword, provide functionality for overriding the inferred or declared type of a value. However, TypeScript has certain limitations on type assertions. Specifically, TypeScript prohibits as
casting when narrowing a type through control flow analysis:
function processShape(shape: Shape) { if ("width" in shape) { const rectangle = shape as Rectangle; // Process rectangle } else { const square = shape as Square; // Process square } }
TypeScript will raise an error because it cannot narrow the type of shape
based on the type assertions. To overcome this limitation, you can introduce a new variable within each branch of the control flow:
function processShape(shape: Shape) { if ("width" in shape) { const rectangle: Rectangle = shape; // Process rectangle } else { const square: Square = shape; // Process square } }
By assigning the type assertion directly to a new variable, TypeScript can correctly infer the narrowed type.
A discriminated union is a type that represents a value that can be of several possibilities. Discriminated unions combine a set of related types under a common parent, where each child type is uniquely identified by a discriminant property. This discriminant property serves as a literal type that allows TypeScript to perform exhaustiveness checking:
type Circle = { kind: 'circle'; radius: number; }; type Square = { kind: 'square'; sideLength: number; }; type Triangle = { kind: 'triangle'; base: number; height: number; }; type Shape = Circle | Square | Triangle;
You’ve defined three shape types: Circle
, Square
, and Triangle
, all collectively forming the discriminated union Shape
. The kind
property is the discriminator, with a literal value representing each shape type.
Discriminated unions become even more powerful when you combine them with type guards. A type guard is a runtime check that allows TypeScript to narrow down the possible types within the union based on the discriminant property.
Consider this function that calculates the area of a shape:
function calculateArea(shape: Shape): number { switch (shape.kind) { case 'circle': return Math.PI * shape.radius ** 2; case 'square': return shape.sideLength ** 2; case 'triangle': return (shape.base * shape.height) / 2; default: throw new Error('Invalid shape!'); } }
TypeScript leverages the discriminant property, kind
, in the switch
statement to perform exhaustiveness checking. If you accidentally omit a case, TypeScript will raise a compilation error, reminding you to handle all possible shape types.
You can use discriminated unions for type casting. Imagine a scenario where you have a generic response
object that can be one of two types: Success
or Failure
. You can use a discriminant property, status
, to differentiate between the two and perform type assertions accordingly:
type Success = { status: 'success'; data: unknown; }; type Failure = { status: 'failure'; error: string; }; type APIResponse = Success | Failure; function handleResponse(response: APIResponse) { if (response.status === 'success') { // Type assertion: response is of type Success console.log(response.data); } else { // Type assertion: response is of type Failure console.error(response.error); } } const successResponse: APIResponse = { status: 'success', data: 'Some data', }; const failureResponse: APIResponse = { status: 'failure', error: 'An error occurred', }; handleResponse(successResponse); // Logs: Some data handleResponse(failureResponse); // Logs: An error occurred
The status
property is the discriminator in the program above. TypeScript narrows down the type of the response
object based on the status
value, allowing you to safely access the respective properties without the need for explicit type checks:
satisfies
operatorThe satisfies
operator is a new feature in TypeScript 4.9 that allows you to check whether an expression’s type matches another type without casting the expression. This can be useful for validating the types of your variables and expressions without changing their original types.
Here’s the syntax for using the satisfies
operator:
expression satisfies type
And here’s a program that checks if a variable is greater than five with the satisfies
operator:
const number = 10; number satisfies number > 5;
The satisfies
operator will return true
if the expression’s type matches, and false
if otherwise. The satisfies
operator is a powerful tool for improving the type safety of your TypeScript code. It is a relatively new feature, so it is not yet as widely used as other TypeScript features. However, it is a valuable tool that can help you to write more reliable and maintainable code.
In data manipulation, you’ll always need to transform data from one type to another, and the two common transformations you will run into are casting a string to a number or converting a value to a string. Let’s look at how to approach each one.
There are several ways to cast a string to a number in TypeScript:
Using the Number()
function:
let numString: string = '42'; let num: number = Number(numString);
Using the unary +
operator:
let numString: string = '42'; let num: number = +numString;
Using parseInt()
or parseFloat()
:
let intString: string = '42'; let int: number = parseInt(intString); let floatString: string = '3.14'; let float: number = parseFloat(floatString);
parseInt()
and parseFloat()
are more flexible as they allow extracting a number from a string that also includes non-numeric characters. Also, it is good to note that all of these methods will yield NaN
(Not a Number) if the string cannot be parsed as a number.
You can use the String()
function or the toString()
method to convert a value to a string in TypeScript:
let num: number = 42; let numString: string = String(num); // or let numString2: string = num.toString(); let bool: boolean = true; let boolString: string = String(bool); // or let boolString2: string = bool.toString();
Both String()
and toString()
work on essentially any type and convert it to a string representation.
toString()
is a method on the object itself, while String()
is a global function. In most cases, they will yield the same result, but toString()
allows customizing the string representation by overriding the method on custom types:
class CustomType { value: number; constructor(value: number) { this.value = value; } toString() { return `CustomType: ${this.value}`; } } let custom = new CustomType(42); console.log(String(custom)); // Output: [object Object] console.log(custom.toString()); // Output: CustomType: 42
In the above snippet, String(custom)
doesn’t have any special behavior for our CustomType
, whereas custom.toString()
uses our custom implementation.
In this article, you learned about the various ways to perform type casting in TypeScript, including type assertion with the as
operator, type conversion using built-in methods like String()
, Number()
, and Boolean()
, and the subtle differences between type assertion and type casting.
You also learned about concepts like type guards and discriminated unions, which allow you to narrow down the specific type within a union type based on runtime checks or discriminant properties. With these techniques, you can efficiently improve the type safety of your programs and catch potential errors at compile-time.
LogRocket is a frontend application monitoring solution that lets you replay problems as if they happened in your own browser. Instead of guessing why errors happen or asking users for screenshots and log dumps, LogRocket lets you replay the session to quickly understand what went wrong. It works perfectly with any app, regardless of framework, and has plugins to log additional context from Redux, Vuex, and @ngrx/store.
In addition to logging Redux actions and state, LogRocket records console logs, JavaScript errors, stacktraces, network requests/responses with headers + bodies, browser metadata, and custom logs. It also instruments the DOM to record the HTML and CSS on the page, recreating pixel-perfect videos of even the most complex single-page and mobile apps.
Would you be interested in joining LogRocket's developer community?
Join LogRocket’s Content Advisory Board. You’ll help inform the type of content we create and get access to exclusive meetups, social accreditation, and swag.
Sign up nowSOLID principles help us keep code flexible. In this article, we’ll examine all of those principles and their implementation using JavaScript.
JavaScript’s Date API has many limitations. Explore alternative libraries like Moment.js, date-fns, and the new Temporal API.
Explore use cases for using npm vs. npx such as long-term dependency management or temporary tasks and running packages on the fly.
Validating and auditing AI-generated code reduces code errors and ensures that code is compliant.