TypeScript
57 flashcards · Programming Languages · by Orlando Flores
The simplest datatype is the true/false value, which TypeScript calls a boolean value.
let isDone: boolean = false;
let isDone: boolean = false;
The simplest datatype is the true/false value, which TypeScript calls a boolean value.
All numbers in TypeScript can be either floating point values or BigIntegers.
let decimal: number = 6; let hex: number = 0xf00d; let binary: number = 0b1010; let octal: number = 0o744; let big: bigint = 100n;
Floating point numbers have the type number, while BigIntegers have the type bigint. You can use decimal, hexadecimal, binary, and octal literals:
let decimal: number = 6; let hex: number = 0xf00d; let binary: number = 0b1010; let octal: number = 0o744; let big: bigint = 100n;
TypeScript supports arrays. You can define an array using either type[] or Array<elemType>
let list: number[] = [1, 2, 3]; // or let list: Array<number> = [1, 2, 3];
let list: number[] = [1, 2, 3];
TypeScript supports arrays. You can define an array using either type[] or Array<elemType>
let list: Array<number> = [1, 2, 3];
TypeScript supports arrays. You can define an array using either type[] or Array<elemType>
let big: bigint = 100n;
Floating point numbers have the type number, while BigIntegers have the type bigint. You can use decimal, hexadecimal, binary, and octal literals.
let octal: number = 0o744;
Floating point numbers have the type number, while BigIntegers have the type bigint. You can use decimal, hexadecimal, binary, and octal literals.
let binary: number = 0b1010;
Floating point numbers have the type number, while BigIntegers have the type bigint. You can use decimal, hexadecimal, binary, and octal literals.
let hex: number = 0xf00d;
Floating point numbers have the type number, while BigIntegers have the type bigint. You can use decimal, hexadecimal, binary, and octal literals.
let decimal: number = 6;
Floating point numbers have the type number, while BigIntegers have the type bigint. You can use decimal, hexadecimal, binary, and octal literals.
Tuples represent arrays with a fixed number of elements of known types.
let x: [string, number]; x = ["hello", 10]; // OK // Incorrect initialization: // x = [10, "hello"]; // Error: Type 'number' is not assignable to type 'string'.
let x: [string, number]; x = ["hello", 10]; // OK
Tuples represent arrays with a fixed number of elements of known types.
Unlike arrays, tuple elements can have different types
let x: [string, number]; x = ["hello", 10]; // OK // Incorrect initialization: // x = [10, "hello"]; // Error: Type 'number' is not assignable to type 'string'.
let color: string = "blue";
Strings represent textual data. You can use single quotes (') or double quotes (") to define string values. Template strings (using backticks) allow for multiline strings and embedded expressions.
let fullName: string = `Bob Bobbington`;
Strings represent textual data. You can use single quotes (') or double quotes (") to define string values. Template strings (using backticks) allow for multiline strings and embedded expressions.
let sentence: string = `Hello, my name is ${fullName}. I'll be ${age + 1} years old next month.`;
Strings represent textual data. You can use single quotes (') or double quotes (") to define string values. Template strings (using backticks) allow for multiline strings and embedded expressions,
TypeScript is a superset of JavaScript that adds static typing, allowing for early error detection and improved code readability.
... typing helps developers adhere to intended data structures and can improve code quality and maintainability.
The any data type in TypeScript allows a variable to act like a JavaScript variable
Enums organize a collection of related values under a single name. There are two main types: numeric enums (where values default to numbers) and string enums (where each member must be explicitly initialized with a string value).
enum StatusCode { Success = 200, NotFound = 404, ServerError = 500 } let myStatus: StatusCode = StatusCode.Success; enum Direction { Up = "UP", Down = "DOWN", Left = "LEFT", Right = "RIGHT" } let myDirection: Direction = Direction.Up;
Union types allow a variable to hold values of multiple types, enhancing flexibility
let value: string | number; value = 'Hello'; // OK value = 42; // OK value = true; // Error: Type 'boolean' is not assignable to type 'string | number'.
A tuple has a fixed number of elements with specified types at specific positions.
let x: [string, number]; x = ["hello", 10]; // OK // Incorrect initialization: // x = [10, "hello"]; // Error: Type 'number' is not assignable to type 'string'.
TypeScript interfaces play a crucial role in defining contracts within your code and ensuring type-checking.
interface LabeledValue { label: string; } function printLabel(labeledObj: LabeledValue) { console.log(labeledObj.label); } let myObj = { size: 10, label: "Size 10 Object" }; printLabel(myObj); // Outputs: "Size 10 Object"
An interface defines the shape of an object, specifying which properties and methods it should have
interface LabeledValue { label: string; } function printLabel(labeledObj: LabeledValue) { console.log(labeledObj.label); } let myObj = { size: 10, label: "Size 10 Object" }; printLabel(myObj); // Outputs: "Size 10 Object"
interface LabeledValue { label: string; } function printLabel(labeledObj: LabeledValue) { console.log(labeledObj.label); } let myObj = { size: 10, label: "Size 10 Object" }; printLabel(myObj); // Outputs: "Size 10 Object"
An interface defines the shape of an object, specifying which properties and methods it should have.
interface LabeledValue { label: string; } function printLabel(labeledObj: LabeledValue) { console.log(labeledObj.label); } let myObj = { size: 10, label: "Size 10 Object" }; printLabel(myObj); // Outputs: "Size 10 Object"
An interface defines the shape of an object, specifying which properties and methods it should have.
interface LabeledValue { label: string; } function printLabel(labeledObj: LabeledValue) { console.log(labeledObj.label); } let myObj = { size: 10, label: "Size 10 Object" }; printLabel(myObj); // Outputs: "Size 10 Object"
An interface defines the shape of an object, specifying which properties and methods it should have.
enum StatusCode { Success = 200, NotFound = 404, ServerError = 500 } let myStatus: StatusCode = StatusCode.Success;
Numeric Enum: In the string enum example, each member is assigned a specific string.
enum Direction { Up = "UP", Down = "DOWN", Left = "LEFT", Right = "RIGHT" } let myDirection: Direction = Direction.Up;
String Enum : In the numeric enum example, each member is assigned a specific number.
let value: string | number; value = 'Hello'; // OK value = 42; // OK value = true; // Error: Type 'boolean' is not assignable to type 'string | number'.
Union types allow a variable to hold values of multiple types, enhancing flexibility
string: Represents textual data, like "Hello, World!".
let greeting: string = "Hello, World!";
number: Represents both integer and floating-point numbers, like 42 or 3.14.
let age: number = 30;
boolean: Represents true/false values.
let isOpen: boolean = true;
null: Represents the absence of any object value.
let empty: null = null;
undefined: Represents uninitialized variables or missing parameters.
let notDefined: undefined;
object: Represents non-primitive types.
let user: object = { name: "Alice", age: 25 };
Array: Represents a list of values of the same type, like number[] or Array<string>.
let numbers: number[] = [1, 2, 3];
Array: Represents a list of values of the same type, like number[] or Array<string>.
let tuple: [string, number] = ["Alice", 25];
Array: Represents a list of values of the same type, like number[] or Array<string>.
let tuple: [string, number] = ["Alice", 25];
Tuple: Represents an array with fixed number of elements whose types are known, like [string, number].
let tuple: [string, number] = ["Alice", 25];
Enum: A way of giving more friendly names to sets of numeric values.
enum Color { Red, Green, Blue } let c: Color = Color.Green;
any: Can represent any JavaScript value with no specific type constraint.
let anything: any = "Hello"; anything = 42; // No error
void: Used for functions that do not return a value.
function warnUser(): void { console.log("This is a warning message"); }
never: Represents values which never occur, for functions that always throw an error or never return.
function error(message: string): never { throw new Error(message); }
In TypeScript, an intersection type combines multiple types into one.
type TypeA = { prop1: string; }; type TypeB = { prop2: number; }; type CombinedType = TypeA & TypeB; let example: CombinedType = { prop1: "Hello", prop2: 123 };
type TypeA = { prop1: string; }; type TypeB = { prop2: number; }; type CombinedType = TypeA & TypeB; let example: CombinedType = { prop1: "Hello", prop2: 123 };
In TypeScript, an intersection type combines multiple types into one.
type TypeA = { prop1: string; }; type TypeB = { prop2: number; }; type CombinedType = TypeA & TypeB; let example: CombinedType = { prop1: "Hello", prop2: 123 };
In TypeScript, an intersection type combines multiple types into one.
Generics in TypeScript allow you to create reusable code components that work with a variety of types instead of a single type.
function identity<T>(arg: T): T { return arg; } let output1 = identity<string>("myString"); let output2 = identity<number>(100);
Generics provide a way to tell the compiler what type of data is being used without specifying the exact type.
function identity<T>(arg: T): T { return arg; } let output1 = identity<string>("myString"); let output2 = identity<number>(100);
Generics are often used with interfaces, classes, and functions.
function identity<T>(arg: T): T { return arg; } let output1 = identity<string>("myString"); let output2 = identity<number>(100);
function identity<T>(arg: T): T { return arg; } let output1 = identity<string>("myString"); let output2 = identity<number>(100);
In this example, <T> is a type variable that captures the type the user provides (e.g., string or number), so that we can use that information later. Here, identity will return the same type as its input. So output1 is of type string and output2 is of type number.
function identity<T>(arg: T): T { return arg; } let output1 = identity<string>("myString"); let output2 = identity<number>(100);
In this example, <T> is a type variable that captures the type the user provides (e.g., string or number), so that we can use that information later. Here, identity will return the same type as its input. So output1 is of type string and output2 is of type number.
function identity<T>(arg: T): T { return arg; } let output1 = identity<string>("myString"); let output2 = identity<number>(100);
In this example, <T> is a type variable that captures the type the user provides (e.g., string or number), so that we can use that information later. Here, identity will return the same type as its input. So output1 is of type string and output2 is of type number.
interface Lengthwise { length: number; } function loggingIdentity<T extends Lengthwise>(arg: T): T { console.log(arg.length); // Now we know it has a .length property, so no more error return arg; } loggingIdentity({ length: 10, value: 3 }); // This works because the object meets the constraint.
In this example, T extends Lengthwise is a type constraint that means “T can be any type that has a length property”. The loggingIdentity function can now be used with any type that satisfies this constraint, and inside the function, it’s safe to access the .length property of arg.
interface Lengthwise { length: number; } function loggingIdentity<T extends Lengthwise>(arg: T): T { console.log(arg.length); // Now we know it has a .length property, so no more error return arg; } loggingIdentity({ length: 10, value: 3 }); // This works because the object meets the constraint.
In this example, T extends Lengthwise is a type constraint that means “T can be any type that has a length property”. The loggingIdentity function can now be used with any type that satisfies this constraint, and inside the function, it’s safe to access the .length property of arg.
In this example, T extends Lengthwise is a type constraint that means “T can be any type that has a length property”. The loggingIdentity function can now be used with any type that satisfies this constraint, and inside the function, it’s safe to access the .length property of arg.
interface Lengthwise { length: number; } function loggingIdentity<T extends Lengthwise>(arg: T): T { console.log(arg.length); // Now we know it has a .length property, so no more error return arg; } loggingIdentity({ length: 10, value: 3 }); // This works because the object meets the constraint.