HomeToolsAbout a20k

Type Conversion

Built-in Conversions

TS provides built-in functions to explicitly convert values between different types

Number() String() Boolean() // example let numericString: string = "42"; let numberValue: number = Number(numericString);

When converting a string to a number, there are a few strategies:

// best way, but only in Typescript Number("string_num")

Below method works and is preferred over native method parseInt()

  • Because parseInt(null) returns NaN but +null returns 0
+"string"

The only drawback is that without knowing the unary operator, it would be confusing for the code reviewers to understand what is happening here

Type Strategies

Handling missing fetch

// fetchedData.values is expected to be an array data type const fetchedValues = fetchedData.values ?? [];

Filtering items based on

const filteredItems = filter(item => !!item);
© VincentVanKoh