Logo
Pattern

Discover published sets by community

Explore tens of thousands of sets crafted by our community.

TypeScript Types

30

Flashcards

0/30

Still learning
StarStarStarStar

Function Types

StarStarStarStar

Function Types enforce a function's parameter and return types. Example: `let add: (baseValue: number, increment: number) => number;`.

StarStarStarStar

Conditional Types

StarStarStarStar

Conditional Types select one of two possible types based on a condition. Example: `type NonNullable<T> = T extends null | undefined ? never : T;`.

StarStarStarStar

Type Inference

StarStarStarStar

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`.

StarStarStarStar

Class

StarStarStarStar

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; } }`.

StarStarStarStar

ReadonlyArray

StarStarStarStar

ReadonlyArray type makes sure the array elements are not modifiable. Example: `let a: ReadonlyArray<number> = [1, 2, 3];`.

StarStarStarStar

Type Aliases

StarStarStarStar

Type Aliases create a new name for a type. Example: `type StrOrNum = string | number;`.

StarStarStarStar

Type Guards

StarStarStarStar

Type Guards conditionally check the type of a variable. Example: `if (typeof x === 'string') { console.log(x.substr(1)); }`.

StarStarStarStar

Tuple

StarStarStarStar

A Tuple type is an array with fixed number and sequence of elements. Example: `let user: [string, number] = ['Alice', 30];`.

StarStarStarStar

Indexable Types

StarStarStarStar

Indexable Types allow for indexing into the object by a specific type. Example: `interface StringArray { [index: number]: string; }`.

StarStarStarStar

Mapped Types

StarStarStarStar

Mapped Types create new types based on old types by iterating over properties. Example: `type Readonly<T> = { readonly [P in keyof T]: T[P]; }`.

StarStarStarStar

Intersection Type

StarStarStarStar

Intersection Types combine multiple types into one. Example: `type Student = Person & Serializable;`.

StarStarStarStar

Generics

StarStarStarStar

Generics provide a way to create reusable, generic functions, interfaces, or classes. Example: `function identity<T>(arg: T): T { return arg; }`.

StarStarStarStar

Module

StarStarStarStar

Modules are TypeScript's way to organize code in separate files and manage dependencies. Example: `import { ZipCodeValidator } from './ZipCodeValidator';`.

StarStarStarStar

Type Compatibility

StarStarStarStar

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;`.

StarStarStarStar

Enum

StarStarStarStar

An Enum is a way to organize a collection of related values. Example: `enum Direction { Up, Down, Left, Right }`.

StarStarStarStar

Enums with Initialized Values

StarStarStarStar

Enums can be initialized with specific values, often numbers or strings. Example: `enum FileAccess { Read = 1, Write = 2, ReadWrite = 3 }`.

StarStarStarStar

Void

StarStarStarStar

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'); }`.

StarStarStarStar

Readonly Properties

StarStarStarStar

Readonly properties in TypeScript cannot be changed once they are set. Example: `interface Point { readonly x: number; readonly y: number; }`.

StarStarStarStar

Abstract Class

StarStarStarStar

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'); } }`.

StarStarStarStar

Partial Types

StarStarStarStar

The `Partial` type turns all properties of a type into optional ones. Example: `type PartialPoint = Partial<Point>;`.

StarStarStarStar

Any Type

StarStarStarStar

The `any` type allows for any JavaScript value with no constraints. Example: `let notSure: any = 4;`.

StarStarStarStar

Optional Properties

StarStarStarStar

Optional Properties in an interface or type may or may not exist on an object. Example: `interface IUser { name: string; age?: number; }`.

StarStarStarStar

Interface

StarStarStarStar

An Interface is a TypeScript structure that defines the shape of an object. Example: `interface IUser { name: string; age: number; }`.

StarStarStarStar

Null and Undefined

StarStarStarStar

In TypeScript, both `null` and `undefined` have their types named `null` and `undefined` respectively. Example: `let u: undefined = undefined; let n: null = null;`.

StarStarStarStar

Union Type

StarStarStarStar

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;`.

StarStarStarStar

Decorators

StarStarStarStar

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; }`.

StarStarStarStar

Type Assertion

StarStarStarStar

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;`.

StarStarStarStar

Never Type

StarStarStarStar

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); }`.

StarStarStarStar

Literal Types

StarStarStarStar

Literal Types allow you to specify the exact value a type must have. Example: `let exact: 'hello' = 'hello';`.

StarStarStarStar

Namespace

StarStarStarStar

Namespaces are used for logical grouping of functionalities and to avoid naming collisions. Example: `namespace Validation { export interface StringValidator { isAcceptable(s: string): boolean; } }`.

Know
0
Still learning
Click to flip
Know
0
Logo

© Hypatia.Tech. 2024 All rights reserved.