3 Primitive Types
3 Primitive Types
Primitive Types
Primitive Types
The primitive types in TypeScript are the Number, Boolean, String, Void, Null, Undefined types
and Enum type.
Number
The Number primitive type is equivalent to JavaScript primitive type number, which represents
double-precision 64-bit floating-point values. The number keyword is used to define Number
type in TypeScript. TypeScript supports decimal, hexadecimal, binary and octal literals.
Boolean
The Boolean primitive type is equivalent to JavaScript primitive type boolean which accepts a
value that is either true or false. The boolean keyword is used to define a Boolean type in
TypeScript.
String
The String primitive type is equivalent to JavaScript primitive type string and represents the
sequence of characters stored as Unicode UTF-16 code units. The string keyword is used to
define String type in TypeScript.
Just like JavaScript, TypeScript also uses double quotes (") or single quotes (') to surround string
data.
Void
https://www.dotnettricks.com/player/typescript/typescript-fundamentals 1/3
4/4/2020 Playing | TypeScipt Fundamentals
The void type is mostly used as a function return type that does not return a value or as a type
argument for a generic class or function.
Null
The Null primitive type is equivalent to JavaScript primitive type null which accepts the one and
only value null. The null keyword is used to define Null type in TypeScript but it is not useful
because you can only assign a null value to it.
The Null type is a subtype of all types except the undefined type. Hence, you can assign nullas a
value to all primitive types, object types, union types, and type parameters.
Undefined
The Undefined primitive type is equivalent to JavaScript primitive type undefined and all
uninitialized variables in TypeScript and JavaScript have one and only value that is undefined. The
undefined keyword is used to define Undefined type in TypeScript but it is not useful because
you can only assign undefined value to it.
The Undefined type is also a subtype of all types. Hence, you can assign undefined as a value to
all primitive types, object types, union types, and type parameters.
https://www.dotnettricks.com/player/typescript/typescript-fundamentals 2/3
4/4/2020 Playing | TypeScipt Fundamentals
Symbols
Primitive Types
A symbol is a new, primitive data type introduced in ES6 just like number and string. A symbol
value is created by calling the Symbol constructor.
1. let s1 = Symbol();
2. let s2 = Symbol("mySymbol");
hasInstance- Used for determining whether an object is one of the constructor’s instance.
match- Used for matching the regular expression against a string.
iterator- Returns the default iterator for an object and called by the for-of loop.
search- Returns the index number within a string that matches the regular expression.
split- Splits a string at the indices that match the regular expression.
Enum
An enum is a way to give friendly names to a set of numeric values. The enum keyword is used to
define the Enum type in TypeScript. By default, the value of enum’s members starts from 0. But
you can change this by setting the value of one of its members.
In the previous example, we can start the enum’s members value from 2 instead of 0.
An enum type can be assigned to the Number primitive type, and vice versa.
https://www.dotnettricks.com/player/typescript/typescript-fundamentals 3/3