Menu
×
   ❮   
HTML CSS JAVASCRIPT SQL PYTHON JAVA PHP HOW TO W3.CSS C C++ C# BOOTSTRAP REACT MYSQL JQUERY EXCEL XML DJANGO NUMPY PANDAS NODEJS R TYPESCRIPT ANGULAR GIT POSTGRESQL MONGODB ASP AI GO KOTLIN SASS VUE DSA GEN AI SCIPY AWS CYBERSECURITY DATA SCIENCE
     ❯   

TypeScript Casting


There are times when working with types where it's necessary to override the type of a variable, such as when incorrect types are provided by a library.

Casting is the process of overriding a type.


Casting with as

A straightforward way to cast a variable is using the as keyword, which will directly change the type of the given variable.

Example

let x: unknown = 'hello';
console.log((x as string).length);
Try it Yourself »

Casting doesn't actually change the type of the data within the variable, for example the following code will not work as expected since the variable x is still holds a number.

let x: unknown = 4;
console.log((x as string).length); // prints undefined since numbers don't have a length

TypeScript will still attempt to typecheck casts to prevent casts that don't seem correct, for example the following will throw a type error since TypeScript knows casting a string to a number doesn't makes sense without converting the data:

console.log((4 as string).length); // Error: Conversion of type 'number' to type 'string' may be a mistake because neither type sufficiently overlaps with the other. If this was intentional, convert the expression to 'unknown' first.
The Force casting section below covers how to override this.


Casting with <>

Using <> works the same as casting with as.

Example

let x: unknown = 'hello';
console.log((<string>x).length);
Try it Yourself »

This type of casting will not work with TSX, such as when working on React files.


w3schools CERTIFIED . 2022

Get Certified!

Complete the TypeScript modules, do the exercises, take the exam and become w3schools certified!!

$45 ENROLL

Force casting

To override type errors that TypeScript may throw when casting, first cast to unknown, then to the target type.

Example

let x = 'hello';
console.log(((x as unknown) as number).length); // x is not actually a number so this will return undefined
Try it Yourself »

TypeScript Exercises

Test Yourself With Exercises

Exercise:

Cast the "unknown" variable myVar as a string, using the as keyword:

let myVar: unknown = "Hello world!";
console.log(.length);

Start the Exercise


×

Contact Sales

If you want to use W3Schools services as an educational institution, team or enterprise, send us an e-mail:
sales@w3schools.com

Report Error

If you want to report an error, or if you want to make a suggestion, send us an e-mail:
help@w3schools.com

W3Schools is optimized for learning and training. Examples might be simplified to improve reading and learning. Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness of all content. While using W3Schools, you agree to have read and accepted our terms of use, cookie and privacy policy.

Copyright 1999-2024 by Refsnes Data. All Rights Reserved. W3Schools is Powered by W3.CSS.