Lembar Curang Tipe TypeScript Tingkat Lanjut (dengan Contoh)

TypeScript adalah bahasa yang diketik yang memungkinkan Anda untuk menentukan jenis variabel, parameter fungsi, nilai yang dikembalikan, dan properti objek.

Berikut lembar contekan TypeScript Type tingkat lanjut dengan contoh.

Mari selami.

  • Jenis Persimpangan
  • Jenis Persatuan
  • Jenis Generik
  • Jenis Utilitas
  • Sebagian
  • Yg dibutuhkan
  • Hanya baca
  • Memilih
  • Menghilangkan
  • Ekstrak
  • Mengecualikan
  • Merekam
  • NonNullable
  • Jenis yang Dipetakan
  • Jenis Pengawal
  • Jenis Bersyarat

Jenis Persimpangan

Jenis persimpangan adalah cara menggabungkan beberapa jenis menjadi satu. Ini berarti Anda dapat menggabungkan tipe A tertentu dengan tipe B atau lebih dan mendapatkan kembali tipe tunggal dengan semua propertinya.

type LeftType = { id: number left: string } type RightType = { id: number right: string } type IntersectionType = LeftType & RightType function showType(args: IntersectionType) { console.log(args) } showType({ id: 1, left: "test", right: "test" }) // Output: {id: 1, left: "test", right: "test"} 

Seperti yang Anda lihat, IntersectionTypegabungkan dua jenis - LeftTypedan RightTypedan gunakan &tanda untuk membuat jenis persimpangan.

Jenis Persatuan

Jenis gabungan memungkinkan Anda memiliki jenis anotasi yang berbeda dalam variabel tertentu.

type UnionType = string | number function showType(arg: UnionType) { console.log(arg) } showType("test") // Output: test showType(7) // Output: 7 

Fungsinya showTypeadalah tipe gabungan yang menerima string dan angka sebagai parameter.

Jenis Generik

Tipe generik adalah cara menggunakan kembali bagian dari tipe tertentu. Ini membantu untuk menangkap tipe yang Tditeruskan sebagai parameter.

function showType(args: T) { console.log(args) } showType("test") // Output: "test" showType(1) // Output: 1 

Untuk membuat tipe generik, Anda perlu menggunakan tanda kurung dan meneruskan Tsebagai parameter.

Di sini, saya menggunakan T(nama terserah Anda) dan kemudian, memanggil fungsi showTypedua kali dengan anotasi tipe yang berbeda karena ini umum - dapat digunakan kembali.

interface GenericType { id: number name: T } function showType(args: GenericType) { console.log(args) } showType({ id: 1, name: "test" }) // Output: {id: 1, name: "test"} function showTypeTwo(args: GenericType) { console.log(args) } showTypeTwo({ id: 1, name: 4 }) // Output: {id: 1, name: 4} 

Di sini, kami memiliki contoh lain yang memiliki antarmuka GenericTypeyang menerima tipe generik T. Dan karena dapat digunakan kembali, kita dapat memanggilnya terlebih dahulu dengan string lalu nomor.

interface GenericType { id: T name: U } function showType(args: GenericType) { console.log(args) } showType({ id: 1, name: "test" }) // Output: {id: 1, name: "test"} function showTypeTwo(args: GenericType) { console.log(args) } showTypeTwo({ id: "001", name: ["This", "is", "a", "Test"] }) // Output: {id: "001", name: Array["This", "is", "a", "Test"]} 

Tipe generik dapat menerima beberapa argumen. Di sini, kami meneruskan dua parameter: Tdan U, lalu menggunakannya sebagai penjelasan tipe untuk properti. Karena itu, sekarang kita dapat menggunakan antarmuka dan menyediakan tipe yang berbeda sebagai argumen.

Jenis Utilitas

TypeScript menyediakan utilitas bawaan praktis yang membantu memanipulasi jenis dengan mudah. Untuk menggunakannya, Anda harus memasukkan tipe yang ingin Anda ubah.

Sebagian

  • Partial

Partial memungkinkan Anda untuk membuat semua properti tipe Topsional. Ini akan menambahkan ?tanda di samping setiap bidang.

interface PartialType { id: number firstName: string lastName: string } function showType(args: Partial) { console.log(args) } showType({ id: 1 }) // Output: {id: 1} showType({ firstName: "John", lastName: "Doe" }) // Output: {firstName: "John", lastName: "Doe"} 

Seperti yang Anda lihat, kami memiliki antarmuka PartialTypeyang digunakan sebagai anotasi tipe untuk parameter yang diterima oleh fungsi showType(). Dan untuk membuat properti menjadi opsional, kita harus menggunakan Partialkata kunci dan memasukkan tipe PartialTypesebagai argumen. Konon, sekarang semua bidang menjadi opsional.

Yg dibutuhkan

  • Required

Tidak seperti Partial, Requiredutilitas membuat semua properti dari tipe yang Tdiperlukan.

interface RequiredType { id: number firstName?: string lastName?: string } function showType(args: Required) { console.log(args) } showType({ id: 1, firstName: "John", lastName: "Doe" }) // Output: { id: 1, firstName: "John", lastName: "Doe" } showType({ id: 1 }) // Error: Type '{ id: number: }' is missing the following properties from type 'Required': firstName, lastName 

The Requiredutilitas akan membuat semua sifat yang diperlukan bahkan jika kita membuat mereka opsional terlebih dahulu sebelum menggunakan utilitas. Dan jika properti dihilangkan, TypeScript akan membuat kesalahan.

Hanya baca

  • Readonly

Jenis utilitas ini akan mengubah semua properti jenis Tagar tidak dapat ditetapkan ulang dengan nilai baru.

interface ReadonlyType { id: number name: string } function showType(args: Readonly) { args.id = 4 console.log(args) } showType({ id: 1, name: "Doe" }) // Error: Cannot assign to 'id' because it is a read-only property. 

Here, we use the utility Readonly to make the properties of ReadonlyType not reassignable. That said, if you try to give a new value to one of these fields, an error will be thrown.

Besides that, you can also use the keyword readonly in front of a property to make it not reassignable.

interface ReadonlyType { readonly id: number name: string } 

Pick

  • Pick

It allows you to create a new type from an existing model T by selecting some properties K of that type.

interface PickType { id: number firstName: string lastName: string } function showType(args: Pick) { console.log(args) } showType({ firstName: "John", lastName: "Doe" }) // Output: {firstName: "John"} showType({ id: 3 }) // Error: Object literal may only specify known properties, and 'id' does not exist in type 'Pick' 

Pick is a bit different from the previous utilities we have already seen. It expects two parameters - T is the type you want to pick elements from and K is the property you want to select. You can also pick multiple fields by separating them with a pipe(|) symbol.

Omit

  • Omit

The Omit utility is the opposite of the Pick type. And instead of selecting elements, it will remove K properties from the type T.

interface PickType { id: number firstName: string lastName: string } function showType(args: Omit) { console.log(args) } showType({ id: 7 }) // Output: {id: 7} showType({ firstName: "John" }) // Error: Object literal may only specify known properties, and 'firstName' does not exist in type 'Pick' 

This utility is similar to the way Pick works. It expects the type and the properties to omit from that type.

Extract

  • Extract

Extract allows you to construct a type by picking properties that are present in two different types. The utility will extract from T all properties that are assignable to U.

interface FirstType { id: number firstName: string lastName: string } interface SecondType { id: number address: string city: string } type ExtractType = Extract // Output: "id" 

Here, we have two types that have in common the property id. And hence by using the Extract keyword, we get back the field id since it's present in both interfaces. And if you have more than one shared field, the utility will extract all similar properties.

Exclude

Unlike Extract, the Exclude utility will construct a type by excluding properties that are already present in two different types. It excludes from T all fields that are assignable to U.

interface FirstType { id: number firstName: string lastName: string } interface SecondType { id: number address: string city: string } type ExcludeType = Exclude // Output; "firstName" | "lastName" 

As you can see here, the properties firstName and lastName are assignable to the SecondType type since they are not present there. And by using the Extract keyword, we get back these fields as expected.

Record

  • Record

This utility helps you to construct a type with a set of properties K of a given type T. Record is really handy when it comes to mapping the properties of a type to another one.

interface EmployeeType { id: number fullname: string role: string } let employees: Record = { 0: { id: 1, fullname: "John Doe", role: "Designer" }, 1: { id: 2, fullname: "Ibrahima Fall", role: "Developer" }, 2: { id: 3, fullname: "Sara Duckson", role: "Developer" }, } // 0: { id: 1, fullname: "John Doe", role: "Designer" }, // 1: { id: 2, fullname: "Ibrahima Fall", role: "Developer" }, // 2: { id: 3, fullname: "Sara Duckson", role: "Developer" } 

The way Record works is relatively simple. Here, it expects a number as a type which is why we have 0, 1, and 2 as keys for the employees variable. And if you try to use a string as a property, an error will be thrown. Next, the set of properties is given by EmployeeType hence the object with the fields id, fullName, and role.

NonNullable

  • NonNullable

It allows you to remove null and undefined from the type T.

type NonNullableType = string | number | null | undefined function showType(args: NonNullable) { console.log(args) } showType("test") // Output: "test" showType(1) // Output: 1 showType(null) // Error: Argument of type 'null' is not assignable to parameter of type 'string | number'. showType(undefined) // Error: Argument of type 'undefined' is not assignable to parameter of type 'string | number'. 

Here, we pass the type NonNullableType as an argument to the NonNullable utility which constructs a new type by excluding null and undefined from that type. That said, if you pass a nullable value, TypeScript will throw an error.

By the way, if you add the --strictNullChecks flag to the tsconfig file, TypeScript will apply non-nullability rules.

Mapped types

Mapped types allow you to take an existing model and transform each of its properties into a new type. Note that some utility types covered earlier are also mapped types.

type StringMap = { [P in keyof T]: string } function showType(arg: StringMap) { console.log(arg) } showType({ id: 1, name: "Test" }) // Error: Type 'number' is not assignable to type 'string'. showType({ id: "testId", name: "This is a Test" }) // Output: {id: "testId", name: "This is a Test"} 

StringMap will transform whatever types that passed in into a string. That said, if we use it in the function showType(), the parameters received must be a string - otherwise, an error will be thrown by TypeScript.

Type Guards

Type Guards allow you to check the type of a variable or an object with an operator. It's a conditional block that returns a type using typeof, instanceof, or in.

  • typeof
function showType(x: number | string) { if (typeof x === "number") { return `The result is ${x + x}` } throw new Error(`This operation can't be done on a ${typeof x}`) } showType("I'm not a number") // Error: This operation can't be done on a string showType(7) // Output: The result is 14 

As you can see, we have a normal JavaScript conditional block that checks the type of the argument received with typeof. With that in place, you can now guard your type with this condition.

  • instanceof
class Foo { bar() { return "Hello World" } } class Bar { baz = "123" } function showType(arg: Foo | Bar) { if (arg instanceof Foo) { console.log(arg.bar()) return arg.bar() } throw new Error("The type is not supported") } showType(new Foo()) // Output: Hello World showType(new Bar()) // Error: The type is not supported 

Like the previous example, this one is also a type guard that checks if the parameter received is part of the Foo class or not and handles it consequently.

  • in
interface FirstType { x: number } interface SecondType { y: string } function showType(arg: FirstType | SecondType) { if ("x" in arg) { console.log(`The property ${arg.x} exists`) return `The property ${arg.x} exists` } throw new Error("This type is not expected") } showType({ x: 7 }) // Output: The property 7 exists showType({ y: "ccc" }) // Error: This type is not expected 

The in operator allows you to check whether a property x exists or not on the object received as a parameter.

Conditional Types

Conditional types test two types and select one of them depending on the outcome of that test.

type NonNullable = T extends null | undefined ? never : T 

This example of the NonNullable utility type checks if the type is null or not and handle it depending on that. And as you can note, it uses the JavaScript ternary operator.

Thanks for reading.

You can find other great content like this on my blog or follow me on Twitter to get notified.