Subscribe to my email list now at http://jauyeung.net/subscribe/
Follow me on Twitter at https://twitter.com/AuMayeung
Many more articles at https://medium.com/@hohanga
Even more articles at http://thewebdev.info/
With dynamically typed languages, it’s hard to determine the type that a variable has without logging it, and we might assign data that we don’t want in the variable.
The largest and smallest available values for a number are
-Infinity, respectively. We can also use the constants
Number.MIN_VALUE to represent the largest and smallest numbers. We can use the
There are also the constants
1/+0 // Infinity 1/-0 // -Infinity
We can declare numbers like in the following code with TypeScript:
const x: number = 1 const y: number = x + 1;
In TypeScript, there is the BigInt type to store numbers that are beyond the safe integer range. A BigInt number can be created by adding an
n character to the end of a number. With BigInt, we can make calculations that have results beyond the safe range of normal numbers. For example, we can write the following expressions and still get the numbers we expect:
const x: bigint = 2n ** 55n; const y: bigint = x + 1n;
x we get
36028797018963968n and for
y we get
36028797018963969n, which is what we expect. BigInts can use the same arithmetic operations as numbers, such as
%. A BigInt behaves like a number when converted to a Boolean, with functions, keywords, or operators like
!. BigInts cannot be operated in the same expressions as numbers. If we try that, we will get a
TypeError. This is enforced with TypeScript compiler checks before compilation is done.
Strings are used to represent textual data. Each element of the string has its own position in the string. It’s zero-indexed, so the position of the first character of a string is 0. The
length property of the string has the total number of characters of the string.
substr() function and use the
concat() function to concatenate two strings.
We should only present text data with strings. If there are more complex structures needed for your data structure, then they shouldn’t be represented with a string. Instead, they should be objects. This is because it’s easy to make mistakes with strings since we can put in the characters we want.
We can declare strings in TypeScript with the following code:
const x: string = 'abc';
Object is a reference data type, which means it can be referenced by an identifier that points to the location of the object in memory. In memory, the object’s value is stored, and, with the identifier, we can access the value. Object has properties, which are key-value pairs with the values being able to contain the data with primitive types or other objects.
That means we can use object to build complex data structures. The key is an identifier for the values of a property, which can be stored as a string or symbol. There are two types of properties that have certain attributes in an object. Objects have data properties and accessor properties.
[[Value]]— This can be of any type. It has the value retrieved by a getter of the property. Defaults to
[[Writable]]— This is a Boolean value. If it’s
[[Value]]can’t be changed. Defaults to
[[Enumerable]]— This is a Boolean value. If it’s
true, then it can be iterated over by the
for...inloop, which is used to iterate over the properties of an object. Defaults to
[[Configurable]]— This is a Boolean value. If it’s
true, then the property can be deleted or changed to an accessor property, and all attributes can be changed. Otherwise, the property can’t be deleted or changed to an accessor property, and attributes other than
[[Writable]]can’t be changed. Defaults to
[[Get]]— This is either a function or it’s
undefined. This may contain a function that is used to retrieve a property value whenever a property is being retrieved. Defaults to
[[Set]]— This is either a function or it’s
undefined. This lets us set the assigned value to an object’s property whenever an object’s property is attempted to be changed. Defaults to
[[Enumerable]]— This is a Boolean value that defaults to
false. If it’s
true, then the property will be included when we loop through the properties with the
[[Configurable]]— This is a Boolean value that defaults to
false. If it’s
false, then we can’t delete the property and can’t make changes to it.
Date object built into the standard library, so we can use it to manipulate dates.
Array are also objects.
length property to get the size of the array. The
Array object has many convenient methods to manipulate arrays, like the
push method to add items to the end of the array and the
indexOf method to find the index of the first occurrence of a given value. A
TypedArray object is an object that lets us see an array-like view of a binary data buffer.
Since ES2015, the following typed array objects are available:
Int8Array, value ranges from -128 to 127
Uint8Array, value ranges from 0 to 255
Uint8ClampedArray, value ranges from 0 to 255
Int16Array, value ranges from -32768 to 32767
Uint16Array, value ranges from 0 to 65535
Int32Array, value ranges from -2147483648 to 2147483647
Unit32Array, value ranges from 0 to 4294967295
Floar32Array, value ranges from -1.2 times 10 to the 38 to 3.4times 10 to the 38
Float64Array, value ranges from 5.0 times 10 to the 324 to 1.8 times 10 to the 308
BigInt64Array, value ranges from -2 to the 63 to 2 to the 63 minus 1
BigUint64Array, value ranges from 0 to 2 to the 64 minus 1
Since ES2015, we have new iterable object types. They are
WeakSet represent sets of objects, and
WeakMap represent objects with a list of key-value pairs.
Map keys can be iterated over, but
WeakMap’s keys cannot be.
With TypeScript, we put constructor names after the colon in the variable declaration to declare their types. For example, if we want to declare a
Map object, we can write:
const map: Map<string, number> = new Map([['a', 1], ['b', 2]]);
Note that the way to declare some objects like
Map objects. With the code above, the keys of the
Map are always strings and the values are always numbers.