Type Casting
Type casting is the process of converting a value from one data type to another. This can be useful when you need to work with a value in a different data type than the one it is currently in.
Type casting in TypeScript is done using the as
keyword. Here is an example of type casting:
const value: any = '123';
// Cast the value to a number
const numberValue = value as number;
console.log(numberValue); // Output: 123
In this example, the value
variable is cast to a number using the as
keyword. The numberValue
variable now contains the value 123
as a number.
Type casting can also be done with more complex data types, such as structs. Here is an example of type casting an object:
type Person = {
name: string;
age: number;
};
const person: any = {
name: 'Alice',
age: 30,
};
// Cast the person object to the Person type
const typedPerson = person as Person;
console.log(typedPerson.name); // Output: Alice
console.log(typedPerson.age); // Output: 30
In this example, the person
object is cast to the Person
type using the as
keyword. The typedPerson
variable now contains the person
object with the correct type.
Type casting can be a powerful tool when working with Naytive, but it should be used carefully to avoid runtime errors. It is important to ensure that the value being cast is compatible with the target data type to avoid unexpected behavior.
const value: any = '123';
// Cast the value to a boolean
const booleanValue = value as boolean;
console.log(booleanValue); // Error: Type 'string' and type 'boolean' have no overlap
In this example, the value
variable is cast to a boolean, which is not compatible with the string value '123'
. This will result in a runtime error when the code is executed.