Basic Types
Primitive types in TypeScript include number, string, boolean, null, undefined, and symbol. Object types encompass arrays, objects, functions, and more. You can define custom complex object types using type aliases.
// Primitive types: number/string/boolean/null/undefined/symbol
// Object types: arrays, objects, functions, etc.
type MixedArray = (number | string);
let list1: MixedArray = [1, 'a', 3, 'b'];
let list2: MixedArray = ['x', 'y', 6, 7];
// Function types specify parameter and return types
function calculateSum(val1: number, val2: number): number {
return val1 + val2;
}
const multiply = (x: number, y: number): number => {
return x * y;
};
Functions
Optional parameters are denoted with a question mark (?) and must appear at the end of the parameter list.
function processArray(start?: number, end?: number): void {
// Function implementation
}
Variable Declaration and Types
Variables can be declared using let, var, or const. TypeScript supports both primitive and object types. Primitive types include number, string, boolean, undefined, and null. The var keyword has function scope, while let and const have block scope. The any type should be avoided in production code when possible.
// Variable declaration: let variableName: Type
let counter: number = 0;
const message: string = "Hello, TypeScript!";
var globalVar: boolean = true;
Strings
TypeScript recommends using primitive types like number, string, boolean, and symbol instead of their wrapper objects like Number, String, Boolean, etc.
// Converting between strings and numbers
let numericValue: number = parseInt("42");
let stringValue: string = String(123);
// String wrapper object methods
const text = new String("Example");
console.log(text.charAt(0)); // Returns character
console.log(text.charCodeAt(0)); // Returns character code
console.log(text.toLowerCase()); // Converts to lowercase
console.log(text.substring(0, 3)); // Extracts substring
Arrays
TypeScript provides simplified array notation: Array
// Array definitions
let scores: Array<number>;
let items: number[];
const matrix = new Array<number>(5);
const filledArray = new Array<number>(10).fill(-1);
let values: number[] = [1, 2, 3, 4];
const directions = [[1, 0], [-1, 0], [0, 1], [0, -1]];
const cube: number[][][] = [];
// Initialize 3D array
for (let i = 0; i < n; i++) {
cube[i] = new Array<number>(m);
for (let j = 0; j < m; j++) {
cube[i][j] = new Array<number>(1 << 10).fill(0x3f3f3f3f);
}
}
let mixedData: (number | string)[] = [1, 'a', 3, 'b'];
// Common array methods
values[4] = 5; // Assignment
values.push(6); // Add element
values.fill(-1); // Initialize
values.splice(2, 1); // Remove element
const sum = values.reduce((prev, curr) => prev + curr, 0);
Map Objects
TypeScript, being a superset of JavaScript, supports Map objects which provide key-value pair storage.
// Map definitions
let mapping = new Map();
let dictionary: { [key: string]: number } = {};
// Map operations
mapping.set("key", 42);
const value = mapping.get("key");
const exists = mapping.has("key");
Common Mathematical Functions
TypeScript provides access to JavaScript's Math object for mathematical operations.
const maxValue = Math.max(10, 20, 5);
const power = Math.pow(2, 8);
const largestFloat = Number.MAX_VALUE;