Why data has types
Not all data is treated the same. The number 12 and the text "12" look alike, but you can multiply the first and not the second. A data type is the kind of value a piece of data is, and it decides what you can do with it. TypeScript adds a layer on top of JavaScript for exactly this: you can state each variable's type, and the compiler checks your code against it before anything runs.
The basic types
numberholds every kind of number, whole or decimal:3,-7,3.14.stringholds text, a sequence of characters, written in single quotes, double quotes or backticks:"John Doe",'dog'.booleanholds one of two values:trueorfalse, both in lower case.nullstands for "no value" and is writtennull.
There is no separate integer type. 5 and 3.14 are both number values, so myInt and myFloat in this lesson use the same annotation. Related to null is undefined, the value of a variable that has been declared but never given a value. The word None belongs to other languages and means nothing in TypeScript.
Declaring a variable with a type
Write the type after the name, separated by a colon:
let myInt: number = 5;
let myFloat: number = 3.14;
let myString: string = "Hello";
let myBool: boolean = true;
If you leave the annotation out, TypeScript works out the type from the starting value, so let x = 1 is a number too. Writing it out states your intent, and it keeps the compiler from guessing wrong when the starting value is vague.
Asking for a value's type with typeof
The typeof operator reports the type of a value at run time:
console.log(typeof myInt, typeof myFloat, typeof myString, typeof myBool);
number number string boolean
One result surprises people. typeof null gives "object", not "null". This is an old quirk of JavaScript that cannot be fixed without breaking existing programs. To test for null, compare with === instead of using typeof.
One operator, several meanings
The same symbol can do different jobs depending on the types around it. Take a + b. If both are numbers, it adds them. If both are strings, it joins them:
let a = 10, b = -5;
console.log(a + b);
let c = "Hello ", d = "world!";
console.log(c + d);
5
Hello world!
You cannot tell what a line does without knowing the types of its variables, and that is a good reason to have types.
Invalid operations are caught early
Types also stop operations that make no sense. Multiplying two strings is one of them:
let a = "peanut";
let b = "butter";
console.log(a * b);
The compiler reports an error on this line, saying saying that the left-hand side of an arithmetic operation must be of type any, number, bigint or an enum type. Plain JavaScript has no such check. The JavaScript that tsc still writes runs, though, and prints NaN, short for "not a number". An error pointing at the exact line is far easier to deal with than a NaN that shows up later in the program.
It helps to remember how this works: TypeScript checks types while compiling, and then removes them. The JavaScript that runs has no annotations, so an annotation does not convert anything or check anything at run time.
The lesson's exercise
The exercise builds four variables, one per step, each on its own line and each keeping the lines above it.
- An integer. Declare
myIntwith the typenumberand a whole number such as5. - A decimal. Declare
myFloatwith the samenumbertype and a value with a decimal point, such as3.14. - Text. Declare
myStringwith the typestringand a value in quotes, such as"Hello". - A boolean. Declare
myBoolwith the typebooleanand eithertrueorfalse.
let myInt: number = 5;
let myFloat: number = 3.14;
let myString: string = "Hello";
let myBool: boolean = true;
Common mistakes
- Putting quotes on a number or boolean.
"true"is a string, not a boolean. - Capitalizing booleans.
Trueis a different name, and the compiler will not find it. - Using the wrong annotation. A value that does not match the declared type, like
let n: number = "5", is rejected. - Expecting a separate integer type. Whole numbers and decimals are both
number.
Key takeaways
- The four basic types are
number,string,booleanandnull. - The annotation goes after the name:
let myBool: boolean = true;. typeofreports a value's type at run time, andtypeof nullis"object".- The type of the operands decides what
+does. - The compiler reports invalid operations at the exact line.