Ausnahme gefangen: SSL certificate problem: certificate is not yet valid 📌 Utility Types In Typescript

🏠 Team IT Security News

TSecurity.de ist eine Online-Plattform, die sich auf die Bereitstellung von Informationen,alle 15 Minuten neuste Nachrichten, Bildungsressourcen und Dienstleistungen rund um das Thema IT-Sicherheit spezialisiert hat.
Ob es sich um aktuelle Nachrichten, Fachartikel, Blogbeiträge, Webinare, Tutorials, oder Tipps & Tricks handelt, TSecurity.de bietet seinen Nutzern einen umfassenden Überblick über die wichtigsten Aspekte der IT-Sicherheit in einer sich ständig verändernden digitalen Welt.

16.12.2023 - TIP: Wer den Cookie Consent Banner akzeptiert, kann z.B. von Englisch nach Deutsch übersetzen, erst Englisch auswählen dann wieder Deutsch!

Google Android Playstore Download Button für Team IT Security



📚 Utility Types In Typescript


💡 Newskategorie: Programmierung
🔗 Quelle: dev.to

Typescript utility types are a set of built-in type utilities that provide common type transformations. They are designed to help with working with existing types and easily constructing new types. These utility types include Pick, Omit, Exclude, Record, Partial, Readonly, and more. They can be used to create custom types for use in a project.

Utility types are a powerful tool that allow you to create custom types from existing types. They are especially useful when working with complex data structures. For example, you can use Pick to select only certain properties from an existing type, Omit to exclude certain properties from an existing type, Exclude to create a type that excludes certain specific types, Record to create a type of a specific set of keys and corresponding values, Partial to create a type that contains only some of the properties of an existing type, Readonly to make all properties of a type read-only, and more.

Here's a list of all the common utility types that we can utilize for type transformations, note that the utility types are not limited to just the ones listed here.

  • Pick
  • Omit
  • Exclude
  • Record
  • Partial
  • Readonly
  • Constructor
  • NonNullable
  • Required

In no particular order let's start with Pick

Pick

The Pick utility type is used to select only certain properties from an existing type. For example, if you have an interface with multiple properties, you can use Pick to create a new type with only the properties you want.

For example, suppose you have an interface called Person that has the following properties:

interface Person {
  name: string;
  age: number;
  height: number;
  weight: number;
}

You can use Pick to create a new type that only includes the name and age properties:

type PersonInfo = Pick<Person, 'name' | 'age'>;

This creates a new type called PersonInfo that only includes the name and age properties. The Pick utility type should be used when you need to create a new type from an existing type that contains only certain properties. It can be used to create custom types for use in a project, or to simplify existing types for use in other operations.

Omit

The Omit utility type is used to exclude certain properties from an existing type. For example, if you have an interface with multiple properties, you can use Omit to create a new type without the properties you don't want.

For example, suppose you have an interface called Person that has the following properties:

interface Person {
  name: string;
  age: number;
  height: number;
  weight: number;
}

You can use Omit to create a new type that excludes the height and weight properties:

type PersonInfo = Omit<Person, 'height' | 'weight'>;

This creates a new type called PersonInfo that only includes the name and age properties. The Pick and Omit utility types are related in that they both allow you to create custom types from existing types. However, the Pick utility type is used to select only certain properties from an existing type, while the Omit utility type is used to exclude certain properties from an existing type. The Omit utility type should be used when you need to create a new type from an existing type that excludes certain properties.

Required

The Required utility type is used to make all properties of a type required. For example, if you have an interface with multiple optional properties, you can use Required to make all of the properties required.

For example, suppose you have an interface called Person that has the following properties:

interface Person {
  name?: string;
  age?: number;
  height?: number;
  weight?: number;
}

You can use Required to make all of the properties required:

type PersonInfo = Required<Person>;

This creates a new type called PersonInfo that requires all of the properties. The Required utility type should be used when you need to make all of the properties of a type required, or to ensure that all properties of an existing type are used.

Partial

The Partial utility type is used to create a type that contains only some of the properties of an existing type. For example, if you have an interface with multiple properties, you can use Partial to create a new type that includes only a subset of the properties.

For example, suppose you have an interface called Person that has the following properties:

interface Person {
  name: string;
  age: number;
  height: number;
  weight: number;
}

You can use Partial to create a new type that only includes the name and age properties:

type PersonInfo = Partial<Person>;

This creates a new type called PersonInfo that only includes the name and age properties, and all of the properties are optional. The Partial utility type should be used when you need to create a new type from an existing type that contains only some of the properties to make existing types more flexible.

Record

The Record utility type is used to create a type of a specific set of keys and corresponding values. For example, if you have a collection of objects with different keys and values, you can use Record to create a type that only includes the keys and values you need.

For example, suppose you have an array of objects with the following keys and values:

const people = [
  { name: 'John', age: 30 },
  { name: 'Mary', age: 25 },
  { name: 'Alice', age: 20 }
];

You can use Record to create a type that only includes the name and age properties:

type PersonInfo = Record<string, { name: string, age: number }>;

This creates a new type called PersonInfo that only has two properties: name and age. The Record utility type should be used when you need to create a type that has a specific set of keys and corresponding values. is not better to use an interface or a type for this purpose?

It depends on the situation. An interface is usually recommended when you need to define a type that will be used in multiple places and need to ensure that the type has consistent properties across the project. However, a type can be used if you only need to define a type for specific purposes and don’t need the type to be consistent across the project. In addition, the utility types can be used to easily construct custom types that can be used in place of interfaces or types.

Exclude

The Exclude utility type is used to create a type that excludes certain specific types. For example, if you have a union type of multiple possible types, you can use Exclude to create a type that excludes one or more of the types.

For example, suppose you have a union type of string and number:

type StrNumUnion = string | number;

You can use Exclude to create a type that excludes the number type:

type StrOnlyUnion = Exclude<StrNumUnion, number>;

This creates a new type called StrOnlyUnion that only includes the string type. The Exclude utility type should be used when you need to create a type that excludes certain specific types. It can be used to create custom types for use in a project, or to simplify existing types for use in other operations. How is this different from the Omit type? The Omit utility type is used to exclude certain properties from an existing type, while the Exclude utility type is used to create a type that excludes certain specific types.

ReadOnly

The Readonly utility type is used to make all properties of a type read-only. For example, if you have an interface with multiple properties, you can use Readonly to make all of the properties read-only.

For example, suppose you have an interface called Person that has the following properties:

interface Person {
  name: string;
  age: number;
  height: number;
  weight: number;
}

You can use Readonly to make all of the properties read-only:

type PersonInfo = Readonly<Person>;

This creates a new type called PersonInfo that has all of the properties as read-only. The Readonly utility type should be used when you need to make all of the properties of a type read-only.

Constructor

The Constructor utility type is used to create a type that is a constructor function. For example, if you have a class with multiple properties, you can use Constructor to create a type that is a constructor function for the class.

For example, suppose you have a class called Person with the following properties:

class Person {
  name: string;
  age: number;
  height: number;
  weight: number;
}

You can use Constructor to create a type that is a constructor function for the class:

type PersonConstructor = Constructor<Person>;

This creates a new type called PersonConstructor that is a constructor function for the Person class. The Constructor utility type should be used when you need to create a type that is a constructor function for a class.

NonNullable

The NonNullable utility type is used to create a type that is non-nullable. For example, if you have a union type that includes null, you can use NonNullable to create a type that excludes null.

For example, suppose you have a union type that includes null:

type StrNumUnion = string | number | null;

You can use NonNullable to create a type that excludes null:

type StrNumNonNullable = NonNullable<StrNumUnion>;

This creates a new type called StrNumNonNullable that only includes the string and number types. The NonNullable utility type should be used when you need to create a type that excludes null. It can be used to create custom types for use in a project, or to simplify existing types for use in other operations.

...



📌 How Types Work in TypeScript – Explained with JavaScript + TypeScript Code


📈 36.13 Punkte

📌 Utility Types, in Typescript.


📈 35.24 Punkte

📌 Utility Types In Typescript


📈 35.24 Punkte

📌 Typescript utility types that you must know


📈 35.24 Punkte

📌 10 Сustom Utility Types for TypeScript Projects


📈 35.24 Punkte

📌 A little about Typescript Utility Types


📈 35.24 Punkte

📌 Begineer: TypeScript Utility Types


📈 35.24 Punkte

📌 Unraveling TypeScript’s Utility Types


📈 35.24 Punkte

📌 What is Utility Analysis?|Total Utility and Marginal Utility


📈 33.55 Punkte

📌 10 typescript developers you must follow to become typescript expert in 2024


📈 24.16 Punkte

📌 I made "TypeScript Swagger Editor", new type of Swagger UI writing TypeScript code in the browser


📈 24.16 Punkte

📌 Introduction to TypeScript — What is TypeScript?


📈 24.16 Punkte

📌 TypeScript 2.9: Jetzt mit neuen Editor-Features und Import-Types


📈 24.06 Punkte

📌 TypeScript: Static Types for JavaScript


📈 24.06 Punkte

📌 TypeScript: Static Types for JavaScript


📈 24.06 Punkte

📌 TypeScript 3.5 verbessert das Prüfen von Union Types


📈 24.06 Punkte

📌 Programmiersprache: TypeScript 4.1 erweitert Mapped und String Literal Types


📈 24.06 Punkte

📌 Programmiersprache: TypeScript 4.1 erlaubt Templates in String Literal Types


📈 24.06 Punkte

📌 How to Not Lose Typescript Types When Using Custom React Components


📈 24.06 Punkte

📌 Programmiersprache: TypeScript 4.3 verfeinert Arbeit mit Template String Types


📈 24.06 Punkte

📌 Difference between types and interfaces in Typescript


📈 24.06 Punkte

📌 Typescript: Types


📈 24.06 Punkte

📌 Mastering Advanced Types in TypeScript.


📈 24.06 Punkte

📌 Using keyof and typeof for types efficiently in TypeScript


📈 24.06 Punkte

📌 1. Explore basic types of typescript


📈 24.06 Punkte

📌 TypeScript Template Literal Types: Practical Use-Cases for Improved Code Quality


📈 24.06 Punkte

📌 Typescript event types for React


📈 24.06 Punkte

📌 Fastest Way to Auto Generate Types for Typescript and Input validation


📈 24.06 Punkte

📌 Learn Primitives Types in TypeScript


📈 24.06 Punkte

📌 A Quick Introduction To Types In TypeScript


📈 24.06 Punkte

📌 What is the difference between Interfaces vs Types in TypeScript?


📈 24.06 Punkte

📌 You Should Be Using Types Instead Of Interfaces In TypeScript


📈 24.06 Punkte

📌 How to Choose Your Type: TypeScript Interfaces vs Types


📈 24.06 Punkte

📌 Always abstract nested types in TypeScript


📈 24.06 Punkte

📌 What are Types in TypeScript


📈 24.06 Punkte











matomo