5 fundamental types in TypeScript
TypeScript is a JavaScript superset that adds static typing. Knowing and understanding the data types found in the language is crucial for effective programming, enabling better type control and error detection at the compilation stage. In this article, we will examine basic types such as numbers, strings, and boolean values and present methods for declaring variables. Below are several basic data types inherited from JavaScript and available in TypeScript:
number
: Represents numeric values, both integer and floating point.string
: Represents sequences of characters.boolean
: Represents a logical value, which can betrue
orfalse
.any
: Represents any data type. Assigning a value to theany
type avoids type-checking during compilation.object
: Represents a complex object that can contain various properties and methods.
In addition, TypeScript also introduces new data types, such as:
enum
: Represents a set of named values.tuple
: Represents an ordered sequence of elements with different types.null
iundefined
: Represents the special valuesnull
andundefined
, which denote the absence of a value.array
: Represents an array of values. In TypeScript, an array can be defined usingtype[]
notation, such asnumber[]
orstring[]
.function
: Represents a function. In TypeScript, functions can have a specific argument type and return type.
Tired of reading? Watch this video to see how Wojtek makes things quick and easy (available in Polish only).
When assigning a value to a newly declared variable, we do not need to specify its type, because TypeScript will infer the variable's type on its own. On the other hand, if we do not know the value of the variable at the time of declaration, TypeScript will use the type any, which accepts any other data type into its structure.
let petFavoriteFood
//After displaying console.log, we see:
let petFavoriteFood: any
The any
type should be avoided because of later possible compilation errors caused by a lack of type checking. Using the type any
eliminates one of the most important benefits of using TypeScript, namely static typing. Static typing allows type errors to be detected at compile time before the code is executed. Using any
eliminates this check, which can lead to errors during program execution. In addition, when you use type any
, you lose the ability to autocomplete and hint. One of the advantages of TypeScript is the intelligent autocompletion and hints available in code editors.
Wanting to specify the type of a variable when declaring it, we specify the type name after a colon.
let petFavoriteFood: string
Wanting to assign a variable petFavoriteFood a value of a type other than:string
, TypeScript will return a message:
let petFavoriteFood: string
petFavoriteFood = 5 Type number is not assignable to type string
If we change the string
type to number
the compiler will not return an error.
let petFavoriteFood: number
petFavoriteFood = 5
These are just a few of the basic data types available in TypeScript. TypeScript also has many more advanced functions and types that allow you to define data types more precisely in your code. The other types will be presented in the following articles. Taking a look at the data types available in TypeScript and using them consciously will contribute to creating more reliable and scalable applications.