TypeScript Basic Types
Like any other languages, TypeScript types are divided into different categories. All types in TypeScript are subtypes of a single top type called the Any type.
Any Type
The Any type is representing any JavaScript value with no constraints. If you don't know about the types, you can use any type to hold any type that are assigning dynamically.
TypeScript Types Example 1
The other types in TypeScript are categorized into
- Primitive Types
- Object Types
- Union Types
- Intersection Types
- Types parameters
Primitive Types
The primitive types includes Number, Boolean, String, Symbol, Void, Null, and Undefined types along with user defined enum types. Each primitive type uses specific keywords in programming such as number, boolean, string, symbol, and void respectively.
1. The Number Type
The number datatype is a primitive type and represents double-precision or floating point values.
TypeScript Number Type Example 2
Try it Yourself
2. The Boolean Type
This is very simple datatype which is used to hold true/false value and both JavaScript and TypeScript call as boolean value.
TypeScript Boolean Type Example 3
Try it Yourself
3. The String Type
The string datatype, like other languages is used to hold text data with double quotes (") or single quotes (') to surround text data.
TypeScript String Type Example 3
Try it Yourself
4. The Symbol Type
The symbol datatype is primitive type and represents unique tokens that may be used as keys for object properties.
TypeScript Symbol Type Example 3
Try it Yourself
5. The Void Type
The void datatype is represent absence of having any type and commonly used as return type.
TypeScript Void Type Example 3
Try it Yourself
6. The Null Type
The null datatype is references the one and only value of the Null type and considered a valid value for all primitive types.
TypeScript Null Type Example 3
Try it Yourself
7. The Undefined Type
The undefined datatype is used to uninitialized variables.
TypeScript Undefined Type Example 3
Try it Yourself
8. The Enum Type
The enum datatype, like other languages, is used to give more friendly names to sets of numeric values.
TypeScript Enum Type Example 3
Try it Yourself
Other types are covered in other sections