let x: number = 4;
const x: number = 2;
let x = `Hello ${firstName} ${lastName}`; // template string
let x = (condition) ? valueIfTrue:valueIfFalse; // conditional operator
let x = myEntity!.name; // non-null assertion operator
// Basic types
let x: number;
let x: string;
let x: boolean;
let x: any;
let x: void;
let x: null;
let x: undefined;
// Array
let arr: number[] = [1, 2, 3];
let arr: Array<number> = [1, 2, 3];
let first_arr = arr[0];
// Tuple
let tup: [string, number] = ['hello', 4];
// Enum
enum Color { Red, Green, Blue };
let green: Color = Color.Green;
class Person {
first_name: string;
last_name: string;
constructor() {}
greet(): void {
console.log('Hello', this.first_name);
}
}
// Instantiation
let person = new Person();
try {
// code to try
}
catch (err) {
// code to handle errors
if (err instanceof Error) {
} else {
throw(err);
}
}
finally {
// code to be executed regardless of the try-catch result
}