Utility Types · TypeScript

Introduction #TypeScript provides several utility types to facilitate common type transformations. These utilities are available globally.Partial<T>Readonly<T>Record<K,T>Pick<T,K>Omit<T,K>Exclude<T,U>Extract<T,U>NonNullable<T>Parameters<T>ConstructorParameters<T>ReturnType<T>InstanceType<T>Required<T>ThisParameterTypeOmitThisParameterThisType<T>Partial<T> #Constructs a type with all properties of T set to optional. This utility will return a type that represents all subsets of a given type.Example #interface Todo { title: string; description: string; } function updateTodo(todo: Todo, fieldsToUpdate: Partial<Todo>) { return { ...todo, ...fieldsToUpdate }; } const todo1 = { title: 'organize desk', description: 'clear clutter', }; const todo2 = updateTodo(todo1, { description: 'throw out trash', }); Readonly<T> #Constructs a type with all properties of T set to readonly, meaning the properties of the constructed type cannot be reassigned.Example #interface Todo { …

Related Topics: AltJS TypeScript