Explore tens of thousands of sets crafted by our community.
TypeScript Types
30
Flashcards
0/30
Function Types
Function Types enforce a function's parameter and return types. Example: `let add: (baseValue: number, increment: number) => number;`.
Conditional Types
Conditional Types select one of two possible types based on a condition. Example: `type NonNullable<T> = T extends null | undefined ? never : T;`.
Type Inference
TypeScript can infer the type of a variable based on its value if not explicitly defined. Example: `let x = 3; // x is inferred as number`.
Class
Classes in TypeScript are a blueprint for creating objects and provide object-oriented features. Example: `class Greeter { greeting: string; constructor(message: string) { this.greeting = message; } }`.
ReadonlyArray
ReadonlyArray type makes sure the array elements are not modifiable. Example: `let a: ReadonlyArray<number> = [1, 2, 3];`.
Type Aliases
Type Aliases create a new name for a type. Example: `type StrOrNum = string | number;`.
Type Guards
Type Guards conditionally check the type of a variable. Example: `if (typeof x === 'string') { console.log(x.substr(1)); }`.
Tuple
A Tuple type is an array with fixed number and sequence of elements. Example: `let user: [string, number] = ['Alice', 30];`.
Indexable Types
Indexable Types allow for indexing into the object by a specific type. Example: `interface StringArray { [index: number]: string; }`.
Mapped Types
Mapped Types create new types based on old types by iterating over properties. Example: `type Readonly<T> = { readonly [P in keyof T]: T[P]; }`.
Intersection Type
Intersection Types combine multiple types into one. Example: `type Student = Person & Serializable;`.
Generics
Generics provide a way to create reusable, generic functions, interfaces, or classes. Example: `function identity<T>(arg: T): T { return arg; }`.
Module
Modules are TypeScript's way to organize code in separate files and manage dependencies. Example: `import { ZipCodeValidator } from './ZipCodeValidator';`.
Type Compatibility
TypeScript's type compatibility is based on structural subtyping, focusing on the shape that values have. Example: `interface Named { name: string; }; let x: Named; let y = { name: 'Alice', location: 'Seattle' }; x = y;`.
Enum
An Enum is a way to organize a collection of related values. Example: `enum Direction { Up, Down, Left, Right }`.
Enums with Initialized Values
Enums can be initialized with specific values, often numbers or strings. Example: `enum FileAccess { Read = 1, Write = 2, ReadWrite = 3 }`.
Void
The `void` type represents the absence of any value, often used for functions that do not return any value. Example: `function warnUser(): void { console.log('This is a warning message'); }`.
Readonly Properties
Readonly properties in TypeScript cannot be changed once they are set. Example: `interface Point { readonly x: number; readonly y: number; }`.
Abstract Class
Abstract classes are base classes from which other classes may be derived but cannot be instantiated directly. Example: `abstract class Animal { abstract makeSound(): void; move(): void { console.log('Moving along'); } }`.
Partial Types
The `Partial` type turns all properties of a type into optional ones. Example: `type PartialPoint = Partial<Point>;`.
Any Type
The `any` type allows for any JavaScript value with no constraints. Example: `let notSure: any = 4;`.
Optional Properties
Optional Properties in an interface or type may or may not exist on an object. Example: `interface IUser { name: string; age?: number; }`.
Interface
An Interface is a TypeScript structure that defines the shape of an object. Example: `interface IUser { name: string; age: number; }`.
Null and Undefined
In TypeScript, both `null` and `undefined` have their types named `null` and `undefined` respectively. Example: `let u: undefined = undefined; let n: null = null;`.
Union Type
A Union Type is a type formed from two or more other types, representing values that may be any one of those types. Example: `let value: number | string;`.
Decorators
Decorators are special kinds of declarations that can be attached to a class declaration, method, accessor, property, or parameter. Example: `@sealed class Greeter { greeting: string; }`.
Type Assertion
Type Assertion allows you to set the type of a value and tell the compiler not to infer it. Example: `let str: any = '1'; let strLength: number = (<string>str).length;`.
Never Type
The `never` type represents the type of values that never occur, such as functions that do not return because they throw an error. Example: `function throwError(errorMsg: string): never { throw new Error(errorMsg); }`.
Literal Types
Literal Types allow you to specify the exact value a type must have. Example: `let exact: 'hello' = 'hello';`.
Namespace
Namespaces are used for logical grouping of functionalities and to avoid naming collisions. Example: `namespace Validation { export interface StringValidator { isAcceptable(s: string): boolean; } }`.
© Hypatia.Tech. 2024 All rights reserved.