Learn TypeScript Generics In 13 Minutes

Web Dev Simplified
26 Dec 202312:51

Summary

TLDRこのビデオスクリプトは、TypeScriptのジェネリクスに関する深い理解を提供します。ジェネリクスは、関数や型においてデータが複数の異なる型のいずれかである場合に、柔軟性を持たせるために使用されます。ビデオはジェネリクスの基本的な概念、関数での使用、型定義、デフォルト値、および制約について説明し、TypeScriptのより高度な使用法を学ぶためのシンプルで簡潔なアプローチを提供しています。

Takeaways

  • 📌 ジェネリクスは、TypeScriptにおける非常に重要なコンセプトで、関数や型に柔軟性をもたらします。
  • 🔍 関数にジェネリクスを使用すると、異なる型のデータを処理できるようになります(例:配列の最初の要素を返す関数)。
  • 💡 ジェネリック関数を定義する際は、関数名の後に二つの角括弧(`<>`)を置いて、その中にジェネリック型の名前(例:`T`)を記述します。
  • 🌟 TypeScriptは、関数の引数や戻り値からジェネリック型を推論できるため、通常、手動でのジェネリック型の指定は必要ありません。
  • 🛠️ ジェネリクスを使用する際、特定の型システムに従う必要がある場合があります(例:`data` が常にオブジェクトであることが求められる)。
  • 🔧 ジェネリック型はデフォルト値を設定することもでき、これにより、特定の型が指定されなかった場合に備えることができます。
  • 📦 ジェネリック型は、他のジェネリック型をネストしたり、組み合わせたりすることも可能です。
  • 🔑 ジェネリック型を使用すると、APIレスポンスやオブジェクトの型をより正確に定義することができ、これによりコードの安全性が向上します。
  • 🎯 ジェネリクスは、関数や型においてデータが複数の異なる型のいずれかである可能性がある場合に最適な選択です。
  • 📈 ジェネリクスを学ぶことで、TypeScriptを使用する際に柔軟性と精度を高めることができ、より効率的なコードを書くことができます。

Q & A

  • TypeScriptでジェネリックスを学ぶ際に最も怖い部分は何ですか?

    -ジェネリックスを学ぶ際の最も怖い部分は、あちこちにある山括弧や、T、K、Vなどの変数が理解しにくく、これまでの学習内容と大きく異なるためです。

  • ジェネリックスの主な用途は何ですか?

    -ジェネリックスの主な用途は、関数や型の中で使用されるデータが入力や返り値に基づいて型が変わる場合です。

  • ジェネリックスの定義に使用される記号は何ですか?

    -ジェネリックスは、2つの山括弧(<>)を使用して定義されます。

  • なぜジェネリックスを使用するのですか?

    -ジェネリックスを使用することで、関数や型が扱うデータの型を柔軟にし、再利用性と型の安全性を高めることができます。

  • ジェネリックスで複数の型を扱う方法は?

    -ジェネリックスでは、複数の型パラメータをカンマで区切って指定することで、複数の型を扱うことができます。

  • 関数内でジェネリック型を推論する方法は?

    -TypeScriptは、関数に渡された引数の型を基にして、関数内でのジェネリック型を自動的に推論することが多いです。

  • ジェネリック型にデフォルト値を設定することは可能ですか?

    -はい、ジェネリック型にはデフォルト値を設定することが可能で、型パラメータが提供されない場合に使用されます。

  • APIレスポンス型にジェネリックスを使用する利点は何ですか?

    -APIレスポンス型にジェネリックスを使用することで、異なるエンドポイントからのレスポンスの型を柔軟に扱うことができます。

  • ジェネリック型を他の型に拡張することはできますか?

    -はい、ジェネリック型は他の型に拡張することができ、これによりジェネリック型が特定のプロパティやメソッドを持つことを強制することができます。

  • TypeScriptの学習に役立つリソースはありますか?

    -はい、TypeScriptを簡略化したコースがあり、TypeScriptに関する全てを5時間未満で学ぶことができます。

Outlines

00:00

📚 理解TypeScript中的泛型

本段落介绍了TypeScript中泛型的概念和重要性。泛型允许函数或类型接受多种不同类型的数据,而在整个函数或类型的使用过程中保持数据类型的一致性。视频通过一个简单的函数示例,解释了如何使用泛型来处理不同类型的数组,并展示了如何在函数定义中使用尖括号来指定泛型类型。此外,还提到了泛型类型可以有默认值,并且可以通过类型约束来确保数据符合特定的结构。

05:01

🔍 TypeScript中的泛型实例

这一部分通过具体的例子,展示了如何在TypeScript中使用泛型。首先,通过map函数和Map对象的例子,说明了TypeScript能够根据传入的参数自动推断泛型的类型。然后,通过创建一个API响应类型的例子,讲解了如何定义泛型类型,并如何通过泛型参数来指定数据的具体类型。此外,还介绍了如何为泛型类型设置默认值和类型约束,以及如何创建特定类型的别名,使得代码更加清晰易读。

10:01

🎯 泛型在TypeScript中的高级用法

这一段深入探讨了泛型在TypeScript中的高级用法,包括如何为泛型类型设置必须遵守的类型约束。通过API响应数据类型的例子,说明了如何确保泛型数据始终是一个对象,并且如何通过泛型来传递更复杂的类型结构。视频还提到了泛型在函数和类型定义中的常见用途,以及如何通过泛型来提高代码的灵活性和可读性。最后,鼓励观众通过学习课程来更深入地掌握TypeScript和泛型的概念。

Mindmap

Keywords

💡TypeScript

TypeScriptは、JavaScriptのスーパーセットであるオブジェクト指向のプログラミング言語です。このビデオでは、TypeScriptのジェネリクスについて学ぶことができます。TypeScriptを使うことで、コードをより安全に保ちながら、より効率的にアプリケーションを開発することができます。

💡Generics

ジェネリクスは、TypeScriptにおいて特定の型を指定せずに、関数やクラスを定義する方法です。これにより、同じ関数やクラスを使って異なる型のデータを操作できるようになります。ビデオでは、ジェネリクスを用いた関数の作り方や、どのように型を柔軟に扱うことができるかが解説されています。

💡Type Inference

型推論は、TypeScriptが自動的に値の型を推測する機能です。このビデオでは、型推論がジェネリクスと組み合わされた場合、どのように型が推論されるかが説明されています。型推論は、コードの可読性とメンテナンス性を向上させるために重要な役割を果たします。

💡Function

関数は、TypeScriptにおいて繰り返し使用されるコードブロックを定義する方法です。関数は、引数を受け取り、処理を行い、結果を返すことができます。ビデオでは、ジェネリクスを使用して一般的な関数を作成する方法が説明されています。

💡Array

配列は、同じ型のデータの集まりを表すデータ構造です。TypeScriptでは、配列の型を明示的に指定することができ、これにより配列内の全ての要素が同じ型であることが保証されます。ビデオでは、配列とジェネリクスの組み合わせ方について学ぶことができます。

💡Type Parameter

型パラメータは、ジェネリクスにおいて使われる仮の型名です。型パラメータを使用することで、関数やクラスの定義において特定の型を柔軟に指定できます。ビデオでは、型パラメータの使い方と、それを通じて関数や型をより汎用的に利用する方法が説明されています。

💡API Response

APIレスポンスは、Web APIから受け取るデータの形式です。TypeScriptでは、APIレスポンスを表す型を定義し、必要なデータのみを含めることにより、コードの安全性と明確さを高めることができます。ビデオでは、ジェネリクスを使用してAPIレスポンスの型を定義する方法が紹介されています。

💡Type Alias

型エイリアスは、TypeScriptにおいて既存の型に新しい名前を付ける仕組みです。これにより、複雑な型や再利用可能な型を定義することができます。ビデオでは、型エイリアスを使って、特定のAPIレスポンス型を簡潔に表す方法が説明されています。

💡Default Value

デフォルト値は、型パラメータに指定されなかった場合に使用される値です。これにより、ジェネリクスを使用した型定義において、型パラメータに対する明示的な型指定がなくても、適切な型推論が可能になります。ビデオでは、デフォルト値の設定方法と、その利点を説明しています。

💡Constraint

制約は、型パラメータが満たす必要のある条件です。これにより、型パラメータが特定の型であることを強制することができます。ビデオでは、ジェネリクスで型制約を使用して、データの型が特定の構造を持ち続けるように確保する方法が説明されています。

💡Kyle

Kyleは、このビデオの説明者であり、Web開発を簡素化する仕事をしていると述べています。彼はTypeScriptのジェネリクスについて教え、より効率的で安全なコードを書く方法を提供しています。

💡Web Dev Simplified

Web Dev Simplifiedは、このビデオのタイトルであり、Kyleが提供するWeb開発の教育プログラムの名前です。このプログラムは、TypeScriptを含むWeb開発の概念をより簡単に理解し、実践するためのリソースを提供することを目的としています。

Highlights

The video aims to simplify the understanding of TypeScript generics, which can be intimidating for learners due to the prevalence of angle brackets and the concept of type variables.

The speaker promises to teach everything needed to know about generics in an easy and concise format, enabling mastery and practical use by the end of the video.

A simple function that gets the first element of an array is used as an example to demonstrate the limitations of non-generic types when dealing with different data types.

The introduction of generics allows a function to handle different data types based on the input provided, without losing type safety.

Defining a generic function involves placing two sets of angle brackets after the function name, where the generic type parameter is specified.

Generics enable the return type of a function to conform to the data type of the input, ensuring type consistency throughout the function's operations.

TypeScript's inference capabilities are highlighted, showing that it can often determine the generic type without the need for manual specification.

The concept of nesting generics is introduced, demonstrating the flexibility and complexity that can be achieved with generics.

Generics are not limited to functions; they can also be used to create generic types, which is useful for defining APIs and other complex data structures.

The video explains how to create specific type aliases using generics, such as 'userResponse', making the code more readable and maintainable.

Default values can be assigned to generic types, allowing for a simplified definition of common data structures, like API responses with a default 'status' property.

Generics can also enforce certain type constraints, ensuring that the data adheres to specific structural requirements, like always being an object.

The speaker emphasizes that while generics can be used in complex ways, most often they are used simply in functions where TypeScript can infer the generic type based on the input and output.

The video concludes with a recommendation for the speaker's TypeScript Simplified course, which promises to teach TypeScript comprehensively in less than 5 hours.

The key takeaway from the video is understanding when to use generics: when dealing with functions or types where the data type may vary but needs to remain consistent within the scope of that function or type.

Transcripts

play00:00

by far the scariest part about learning

play00:02

typescript is generics I mean there's

play00:04

angle brackets everywhere there's these

play00:05

t k v variables nothing really makes

play00:08

sense and it's so much different than

play00:10

everything else up to that point well

play00:12

after this video you'll no longer have

play00:14

to worry about generics because in this

play00:16

video I'm going to teach you every

play00:17

single thing you going to ever need to

play00:19

know about generics in a really easy and

play00:21

concise format so you're going to be

play00:22

mastering and using them by the end of

play00:24

this video and if you enjoy this video

play00:26

and my simplified approach to learning

play00:28

typescript highly recommend checking out

play00:29

my full full typescript simplified

play00:31

course it'll be linked in the

play00:32

description below it's less than 5 hours

play00:34

long and teaches you everything you're

play00:35

ever going to need to know about

play00:36

typescript to start writing it in your

play00:38

own

play00:42

applications welcome back to web dev

play00:44

simplified my name is Kyle and my job is

play00:45

to simplify the web for you so you can

play00:47

start building your dream project sooner

play00:49

and this is probably the first instance

play00:50

where people start to run into the need

play00:52

for generics as you can see we have a

play00:54

really simple function that gets the

play00:56

first element of an array it takes in an

play00:58

array of numbers and you can see it

play00:59

returns the very first element so if we

play01:01

pass it this array called numbers down

play01:03

to it you can see that it's properly

play01:05

returning us to a number this first num

play01:07

variable is typed as a number and

play01:08

there's no errors at all now let's say

play01:11

that we want to do the exact same thing

play01:12

as we're doing here but instead of

play01:14

passing along a number we want to pass

play01:15

along some strings so this array is

play01:17

going to be some strings it doesn't

play01:19

really matter what these values are you

play01:21

can see this is going to be our first

play01:22

string and we're going to pass along the

play01:24

strings into this git first element but

play01:26

of course we're getting an error and

play01:28

that's because this is accepting a

play01:29

number array and not a string array so

play01:32

there's a bit of a problem here we want

play01:33

this to be able to take in any array and

play01:35

return the first element of it but if we

play01:37

type this as an array of any you're

play01:40

going to notice that the return type we

play01:41

get is of type any it's not of type

play01:43

number or string it's just any which is

play01:45

obviously not very useful and if we

play01:47

tried to type this as like number or

play01:50

string for example like this now you're

play01:53

going to see our return type is either a

play01:54

string or a number even though we know

play01:56

in this first case it should be a number

play01:58

and we know in this second case it

play01:59

should be a string but types scripts not

play02:01

smart enough to know that this is where

play02:03

the idea of a generic type comes in

play02:05

anytime that you have a function that

play02:07

you know has some type of data inside of

play02:09

it where you want that data to be

play02:11

changing its type based on what you pass

play02:13

in or based on what you return that is

play02:15

the perfect case for a generic so here

play02:17

instead of typing out the type we're

play02:19

just going to put any for now but we're

play02:20

going to replace this with a generic

play02:21

type which is based on whatever we pass

play02:23

in so to define a function as a generic

play02:26

function you put two sets of angle

play02:27

brackets afterwards an opening and

play02:28

closing angle brackets and in here you

play02:31

just give it the name of what you want

play02:32

this generic type to be it can be

play02:33

anything you often see this called T but

play02:36

we can call it like element type or

play02:38

something like that to really describe

play02:40

what this is and I'd recommend being as

play02:41

descriptive as you can especially when

play02:43

you're getting started so now we have a

play02:45

special type this type can be literally

play02:47

anything but it's going to be that exact

play02:49

same thing everywhere inside this

play02:50

function so we know this element type is

play02:52

going to be for our array so our array

play02:54

is of this element type now you notice

play02:56

just by doing that we have no more

play02:58

errors in our code which is great and

play02:59

when I hover over my first number here

play03:01

you can see that it actually Returns the

play03:03

type of number and when I hover over my

play03:04

first string here it Returns the type of

play03:06

string and the reason why that works if

play03:08

we hover over this function is you can

play03:10

see what happens is when we call this

play03:12

function it's replacing that section in

play03:14

the angle brackets with the type of

play03:16

number so our array is of a number array

play03:19

same thing here down here it's replacing

play03:21

that with a string which means we have a

play03:23

string array instead of a number array

play03:25

so whenever you have a type that needs

play03:26

to conform to the data being passed in

play03:28

or the data being returned or just used

play03:30

in some way in that function you use a

play03:32

generic type like this and you can pass

play03:34

along multiple generic types for example

play03:36

you could have element type two inside

play03:38

of here and this would be a second

play03:40

generic type that you could use

play03:41

somewhere else maybe for a second

play03:42

variable for our case we just have one

play03:44

single generic type because that's the

play03:45

most common and simple use case and the

play03:47

way that this is working is that

play03:49

typescript is smart enough to know okay

play03:50

this numbers object is a number array

play03:52

and we know this array is of some type

play03:54

and it's an array so typescript is smart

play03:56

enough to know that this element type

play03:57

should be a number if it's not smart

play03:59

enough to know that you can actually

play04:00

manually pass this in when you call a

play04:02

function so if you call a generic

play04:04

function you can actually pass in what

play04:05

the generic value should be to help

play04:07

typescript in for those values if it

play04:09

can't and you just put these angle

play04:10

brackets directly after the function

play04:12

name this is actually something that

play04:14

you've probably even done before if

play04:16

you've ever used something like

play04:17

document. query selector for example if

play04:20

I want to query selector all of my input

play04:22

elements that just have a class of input

play04:24

and I know these are HTML in input

play04:26

elements I could say const input is

play04:28

equal to that and if I try to get the

play04:30

value from my input you notice I get an

play04:32

error because input doesn't have a value

play04:34

property on it because by default input

play04:36

is just an element well I know that this

play04:38

is an input element so I can actually

play04:40

use this crey selector which is a

play04:43

generic as you can see and I can pass in

play04:45

HTML input element as that generic type

play04:48

and now you can see my input here is an

play04:49

HTML input element which does have that

play04:52

value property which is why I no longer

play04:53

get any errors so generics are all over

play04:55

the place inside of typescript and

play04:57

you've used them without even realizing

play04:59

them many many times again for example

play05:01

the array. map function so if I just say

play05:03

const a equals and I have an array with

play05:06

like 1 2 3 for example and I say a map

play05:09

and I just want to map over these

play05:11

whatever that function is going to do it

play05:12

really doesn't matter when I hover over

play05:13

map you'll notice that this is actually

play05:16

a generic function that takes in a value

play05:18

but typescript is smart enough to infer

play05:20

that so you almost never have to

play05:21

actually pass in what that generic value

play05:23

is going to be for example if I just

play05:25

take in and I return a string from here

play05:28

and I hover over this map you can see it

play05:30

properly inferred that this is returning

play05:32

a string as that generic value another

play05:34

really common place you're going to see

play05:35

generics is inside of like maps and sets

play05:38

so I could say const map equals new map

play05:40

just like this and this map is actually

play05:42

generic so I could pass it in for

play05:43

example it's going to be a string number

play05:45

map and that just means that the very

play05:47

first value for this if I try to like

play05:49

set something inside of here so I come

play05:50

in here and I say set I can set and it

play05:53

first needs to take a string and then

play05:54

secondly is going to take a number as

play05:56

the value that's what this typing right

play05:57

here is doing but if I pass this a

play05:59

default value it's smart enough to

play06:01

actually infer if I pass it a string and

play06:03

a number that this map is of the string

play06:06

number generic type so again that

play06:08

inference is really powerful and this is

play06:10

how you would actually pass along

play06:11

multiple generics as you can see here I

play06:13

just put a comma between them and

play06:15

everything is working just fine you can

play06:17

even Nest generics inside of each other

play06:18

for example let's say I wanted to have a

play06:20

map where the string is the value and it

play06:21

actually maps to another map and that

play06:24

map is just a string number map as well

play06:26

as you can see I have these two levels

play06:28

of nesting and if I remove this code to

play06:29

get rid of those errors you can see that

play06:31

this map is just properly being nested

play06:33

inside of each other so you can pass

play06:34

generics to generics as complicated as

play06:36

you want to get it now this is going a

play06:38

little bit off the deep end into

play06:39

probably more confusing territory than

play06:40

you need so let's take a step back and

play06:42

talk about generic types in the form of

play06:44

types we've talked about generic

play06:45

functions but you can also make a type

play06:47

that is generic as well this is really

play06:49

common with things like an API response

play06:51

so let's say that we have a type called

play06:52

API response and here we have this type

play06:55

and it has some data we don't really

play06:57

know what that data is for now I'll just

play06:58

say any and let's just say that we have

play07:00

like an is error and this is going to be

play07:02

a Boolean so what I can do is I can say

play07:04

I can say my response here is equal to

play07:06

and I have data which is just something

play07:08

I don't know what it is and then I have

play07:10

an is

play07:11

error which is going to be false there

play07:14

we go and if I properly type this as an

play07:16

API response you can see that this is

play07:18

all working fine with no errors and

play07:20

let's say that this is returning to me a

play07:21

user so we're going to give them a name

play07:24

and we're going to give them an age

play07:25

let's just say 28 there we go so this is

play07:26

returning to me a user but the problem

play07:28

is my response this data is just any no

play07:30

matter what it's always going to be that

play07:32

any type which is obviously not ideal

play07:34

instead I want this to be a specific

play07:36

type so we can make this a generic type

play07:39

again you just put the angle brackets

play07:41

after whatever your type is and then you

play07:42

give it whatever you want that generic

play07:43

type to be called in our case we'll just

play07:45

call this data and then what I can do is

play07:47

I can replace that any with data and now

play07:49

I'm saying that my data is of the type

play07:51

data and immediately I'm getting an

play07:52

error down here because my API response

play07:54

is accepting a type parameter that

play07:56

generic and I haven't passed in a

play07:58

generic so I need to give it a generic

play08:00

because it can't infer what the generic

play08:01

is it's not smart enough to do that so

play08:03

what I can do is I can say this generic

play08:04

is going to be a name which is a

play08:09

string there we go and an age which is a

play08:12

number and now if I close all that off

play08:14

and save you can see now everything is

play08:16

working and when I hover over my

play08:17

response you can see it's an API

play08:18

response where the name is a string and

play08:20

the age is a number so it has all the

play08:22

properties for is error and then this

play08:24

data type is just inferred from this

play08:26

generic that I passed in and I had to

play08:28

manually pass it in cuz it can't do that

play08:30

crazy inference now often times you may

play08:32

want to make this a little bit easier to

play08:33

read so I can actually take this and

play08:35

turn it into its own type such as user

play08:37

response I can say type user response is

play08:40

equal to this type right here and it's

play08:42

just allowing me to create an alias for

play08:43

that type which makes my code a little

play08:45

bit easier to read now the reason this

play08:46

is so powerful is because if I wanted I

play08:48

could create a brand new type for

play08:50

example if I have a Blog that returns

play08:51

some data I could get a Blog response

play08:53

and if I copy this down and make sure

play08:55

that this blog response for example is

play08:57

just a title with a string you can now

play08:59

now see that in this response for my

play09:01

blog if I make this a Blog response my

play09:04

data right here will now give me an

play09:05

error until I give it a title which is a

play09:08

string and then my errors go away this

play09:10

is a really great way for me to expand

play09:12

upon this I have a generic API response

play09:14

that takes in this data and then I can

play09:15

make specific versions such as a user

play09:17

response or a Blog response and so on

play09:20

that really narrow down what the type of

play09:22

that data is going to be now let's just

play09:23

assume a lot of the API endpoints I have

play09:26

don't really have very complicated data

play09:27

and instead the data is just a status so

play09:30

for example I have a response that looks

play09:31

like this where I have a status which is

play09:34

just a number like that and I have a lot

play09:36

of API responses that look like that and

play09:38

by default they all look like that

play09:39

unless I specify something specific what

play09:42

I can do is I can actually give this a

play09:43

default value so if I just say equals

play09:45

here

play09:46

status number I'm now saying that by

play09:49

default if I don't pass anything inside

play09:51

these angle brackets my data is going to

play09:53

be of the type in object with the status

play09:56

property that is a value of number so

play09:58

now what I can do is I can just clean up

play10:00

my code quite a bit let's just get rid

play10:01

of everything except for this one

play10:03

response that we have right here instead

play10:05

of making this a specific response I'm

play10:06

going to make it an API response and

play10:08

this API response expects a status

play10:10

property which is a number to be passed

play10:12

along by default so since I didn't pass

play10:15

along in generic it used this default

play10:17

value right here but if I want I could

play10:18

override that by saying for example name

play10:21

String and now I need to pass along a

play10:23

name which is a string in order to make

play10:24

typescript happy and of course clean up

play10:26

my code there we go now the final kind

play10:28

of major thing you can do with generics

play10:30

is you can actually give them a type

play10:32

that they must adhere to for example

play10:34

what I could do is I could say you know

play10:35

what I want my data to be a string and

play10:37

this is going to work just fine so for

play10:39

example I can pass along a string and

play10:40

everything's working just fine but I

play10:43

know for a fact that my API response

play10:45

data must always be an object it can't

play10:47

be a string or a number it must always

play10:49

be an object well to get around that

play10:51

what I can do here is I can say that

play10:53

this actually extends a specific type in

play10:55

my case object you could make it more

play10:57

specific less specific but I'm saying

play10:58

that this data must always be an object

play11:01

now we get an error here because string

play11:03

does not extend object this must be an

play11:05

object for example a name that has

play11:07

string and now you can see that if I

play11:10

type in the name that's a string that is

play11:11

now working just fine I can even combine

play11:14

this together with a default value by

play11:15

just coming in here and putting an equal

play11:17

sign for status which is a number and

play11:20

now if they don't pass anything in here

play11:21

at all it defaults to this status number

play11:24

so if I change this to status of 200 you

play11:27

can see I no longer have any errors now

play11:29

these last few things that I covered are

play11:31

really some of the more complicated use

play11:32

cases for a generic in 90% of your cases

play11:35

you're not going to deal with extending

play11:37

you're not going to deal with default

play11:38

values you're just going to have a

play11:39

single generic maybe two generics and

play11:41

you're just going to use them in a

play11:43

couple very sparingly places you may use

play11:45

them in types but most often you're

play11:46

probably going to be using them in

play11:48

functions in which case you generally

play11:50

don't even have to pass along the actual

play11:51

generic itself because inside of

play11:53

functions it's almost always smart

play11:55

enough to infer what that generic type

play11:57

is based on the inputs and outputs of

play11:59

that function and I know I've covered a

play12:00

ton in a really short time so if you

play12:02

need to rewatch this video I 100%

play12:04

recommend it but the key to take away

play12:06

with generics is when you want to create

play12:07

a function or a type where some piece of

play12:10

data inside of it could be multiple

play12:12

different types but during the entire

play12:14

iteration of that type or function you

play12:15

want it to be the same thing but for

play12:17

example it could be a number one time or

play12:18

a string the next time depending on what

play12:20

array you pass it like in our first

play12:21

example that is the perfect use case for

play12:23

a generic and if you enjoyed this video

play12:26

and you like this short simple concise

play12:28

to the point x explanation of everything

play12:29

important about generics you're going to

play12:31

love my typescript simplified course CU

play12:33

I do this exact same thing for every

play12:35

single typescript concept out there

play12:37

giving you everything you need to know

play12:38

about typescript in less than 5 hours so

play12:40

if you want to check that out it's going

play12:41

to be linked down in the description

play12:42

below and if you take that course you're

play12:44

going to be able to use typescript by

play12:45

the end of today which is just

play12:46

absolutely amazing so with that said

play12:49

thank you very much for watching and

play12:50

have a good day

Rate This

5.0 / 5 (0 votes)

相关标签
TypeScriptジェネリクスプログラミング学習コード柔軟性安全性開発技術教育
您是否需要英文摘要?