انتقل إلى المحتوى الرئيسي

Type conversion

Types belonging to the same category as well as different currencies can be converted to each other. Type conversion (typecasting) is the process of converting (converting) a value of one type to a value of another type.

Type conversions are divided into explicit and implicit conversions.

Implicit conversions

Implicit conversions do not require any language constructs and do not lead to data loss. An example of an implicit conversion is converting a value of type int to type double. Being a 64-bit real type, double can store any value of a 32-bit integer type int.

inttodouble

As shown in the image above, converting an int to a double does not result in data loss, since a 32-bit value fits into 64 bits.

Explicit conversions

Explicit conversions, often referred to as typecasting for clarity, take place with developer input and require the specification of language constructs called cast operators. Typecasting is required when data may be lost during the conversion process or, for some reason, the process may end with an error. The simplest example of an explicit conversion is the conversion of the double type, used to store 64-bit floating point values, to the integer type int.

doubletoint

Examples of

TypeScript provides built-in functions for performing type conversions.

You can convert a number to a string using the string constructor as shown below.

let number: number = 42
let numberAsString: string = String(number) //Перевод числа в строку

Alternatively, you can convert a string to a number.

let stringAsNumber: string = '42'
let number: number = Number(stringAsNumber)
  1. Metanit
  2. Xsltdev

Contributors ✨

Thanks goes to these wonderful people (emoji key):


Philipp Dvinyaninov


Dmitriy Vasilev

💵

EnglishMoji!