TypeScript Basic Data Types
Boolean
The most basic datatype is the simple true/false value, which JavaScript and TypeScript call a boolean
value.
let isDone: boolean = false;
Example
function f() {
let message : boolean = true;
return message;
}
Number
As in JavaScript, all numbers in TypeScript are floating point values. These floating point numbers get the type number
.
let decimal: number = 6;
let hex: number = 0xf00d;
let binary: number = 0b1010;
let octal: number = 0o744;
String
TypeScript uses double quotes ("
) or single quotes ('
) to surround string data.
let color: string = "blue";
color = 'red';
let sentence: string = "Hello, my name is " + fullName + ".\n\n" + "I'll be " + (age + 1) + " years old next month.";
Template String
You can also use template strings, which can span multiple lines and have embedded expressions. These strings are surrounded by the backtick/backquote (`
) character, and embedded expressions are of the form ${ expr }
.
let sentence: string = `Hello, my name is ${ fullName }.
I'll be ${ age + 1 } years old next month.`;
Array
TypeScript, like JavaScript, allows you to work with arrays of values.Array types can be written in one of two ways.
let list: number[] = [1, 2, 3];
let list: Array<number> = [1, 2, 3];
Tuple
Tuple types allow you to express an array where the type of a fixed number of elements is known, but need not be the same. For example, you may want to represent a value as a pair of a string
and a number.
// Declare a tuple type
let x: [string, number];
// Initialize it
x = ["hello", 10]; // OK
// Initialize it incorrectly
x = [10, "hello"]; // Error
Enum
As in languages like C#, an enum is a way of giving more friendly names to sets of numeric values.
enum Color {Red, Green, Blue}
let c: Color = Color.Green;
By default, enums begin numbering their members starting at 0
. You can change this by manually setting the value of one of its members. For example, we can start the previous example at 1
instead of 0
:
enum Color {Red = 1, Green, Blue}
let colorName: string = Color[2];
alert(colorName); // Displays 'Green' as it's value is 2 above
Any
The any
type is a powerful way to work with existing JavaScript, allowing you to gradually opt-in and opt-out of type-checking during compilation.You might expect Object
to play a similar role, as it does in other languages. But variables of type Object
only allow you to assign any value to them – you can’t call arbitrary methods on them, even ones that actually exist.
let notSure: any = 4;
notSure.ifItExists(); // okay, ifItExists might exist at runtime
notSure.toFixed(); // okay, toFixed exists (but the compiler doesn't check)
let prettySure: Object = 4;
prettySure.toFixed(); // Error: Property 'toFixed' doesn't exist on type 'Object'.
Void
void
is a little like the opposite of any
.You may commonly see this as the return type of functions that do not return a value.
function warnUser(): void {
alert("This is my warning message");
}
Null and Undefined
In TypeScript, both undefined
and null
actually have their own types named undefined
and null
respectively.
// Not much else we can assign to these variables!
let u: undefined = undefined;
let n: null = null;
Never
The never
type represents the type of values that never occur. For instance, never
is the return type for a function expression or an arrow function expression that always throws an exception or one that never returns; Variables also acquire the type never
when narrowed by any type guards that can never be true.
// Function returning never must have unreachable end point
function error(message: string): never {
throw new Error(message);
}
// Inferred return type is never
function fail() {
return error("Something failed");
}
// Function returning never must have unreachable end point
function infiniteLoop(): never {
while (true) { }
}
Type assertions(Type Casting)
A type assertion is like a type cast.
Type assertions have two forms. One is the “angle-bracket” syntax:
let someValue: any = "this is a string";
let strLength: number = (<string>someValue).length;
let someValue: any = "this is a string";
let strLength: number = (someValue as string).length;