You are reading the article Learn How Cast Method Works In Typescript? updated in September 2023 on the website Happystarlongbien.com. We hope that the information we have shared is helpful to you. If you find the content interesting and meaningful, please share it with your friends and continue to follow and support us for the latest updates. Suggested October 2023 Learn How Cast Method Works In Typescript?
Introduction to TypeScript castThe following article provides an outline for the TypeScript cast. Typecasting, which is also known as type conversion, helps in converting one type into another. Even though they don’t work alike in strong programming languages, it exists. That is, in Object-oriented programming, typecasting exists but in the case of JavaScript, it doesn’t have the typecasting concept. Surprisingly, typescript language that compiles into the programming language JavaScript has the concept of casting. In this article, we will be dealing with typecasting in the typescript programming language.
Start Your Free Software Development Course
Web development, programming languages, Software testing & others
Syntax
let A: number = (B as string).length;
How does the cast method work in TypeScript?As we all know, there will be a particular reason why we choose a programming language.
In the case of Typescript, it is a typed language that boosts the development experience, which is the main reason it is used over JavaScript. The main way it boosts the experience is by catching errors as well as offering fixes before the developer starts deploying code.
Normally, we will assign types like strings, Boolean, numbers, structures, and so forth to different variables, as mentioned below.
let fruit: string = 'apple'; let mark: number = 35; let workdone: boolean = false;Now, let us consider a scenario where we have to assign the variable fruit’s string length to a variable that is initialized as the number. In this situation, what will you do?
let fruit: string = 'apple'; let len: number = ??’This is the situation where we use the typecasting concept. Either typecast the variable explicitly to the string type and find the length. That is, typecasting can be done by prepending the fruit variable using the type enclosed in the angle brackets as depicted below.
let fruit: string = 'apple';Another way to do the typecasting is by adding ‘as’ to the syntax.
let fruit: string = 'apple'; let len: number = (fruit as string).length;Even though both these methods can be used for typecasting, typescript in React will support the typecasting using ‘as’ only. Also, there are some situations where typecasting is not possible with certain types. That is, consider the situation where a number has to be converted to a string, as shown below.
let len: number = 35; let fruit: string =( len as string);If we try to compile this, what will happen?
Yes. The compiler will throw an error as the string type cannot be overlapped to a number.
For solving this, first, we have to typecast lento unknown and then to string as shown below.
let len: number = 35; let fruit: string =( len as unknown) as string;Note:
In typescript, it is possible to downcast as well as upcast the types.
ExamplesIn this section, we will be looking into some working samples on casting in typescript.
Program: Typescript program that casts a string type to number using ‘as’.
let fruit: string = "apple" ; let len: number = (fruit as string).length ; console.log('The length of the string is: ', len) ;In this program, a variable fruit and len are initialized in string and number type, respectively. The length of the string inside the variable fruit is stored in the variable len using the typecasting method. On executing the code, the length of the string will be displayed.
let fruit: string = "apple" ; console.log('The length of the string is: ', len) ;Output :
Program: Typescript program that casts a number type to string using ‘as’.
let len: number = 35; let fruit: string =( len as string); console.log('The length of the string is: ', len);Output:
In this program, a variable fruit and len are initialized in string and number type. But the problem with this program is the type that has to be cast. Here, the number type is trying to cast to a string. But, this will result in an error, as shown below.
let len: number
As a solution for this, we will be modifying the code as shown in the below program.
Program: Typescript program that casts a number type to string using ‘unknown’.
let len: number = 35; let fruit: string =( len as unknown) as string; console.log(fruit);Output:
In this program, the type is set as unknown first, and it is then typecasted. So, the error won’t occur as in the above program output.
ConclusionTypecasting is a technique that helps in converting one type into another. Even though it is not present in javascript, it is surprising that the typescript language that compiles into the programming language JavaScript has the concept of casting. In this article, different aspects such as syntax, working, and examples of typecasting are explained in detail.
Recommended ArticlesWe hope that this EDUCBA information on “TypeScript cast” was beneficial to you. You can view EDUCBA’s recommended articles for more information.
You're reading Learn How Cast Method Works In Typescript?
Update the detailed information about Learn How Cast Method Works In Typescript? on the Happystarlongbien.com website. We hope the article's content will meet your needs, and we will regularly update the information to provide you with the fastest and most accurate information. Have a great day!