Scaler Topics Profile picture
Mar 22 β€’ 21 tweets β€’ 6 min read
Complete TypeScript Guide | Generic Types

Your guide to migrating from JavaScript to TypeScript.

πŸ’™πŸ’™πŸ’™ (Part 3 of 3 Mega 🧡 on TS) Image
Missed part 2?

Here’s it is for those who missed or want to review πŸ‘‡
Type Compatibility

defines which type can be assigned to which type.

Different types which are incompatible cannot be assigned to each other.

See the following example: Image
If a type is a subset of another type, it can be assigned to it.

However, if a type is a superset of another type, it can't be assigned to it ↓ Image
Object Type Compatibility

TypeScript is a structurally-typed language which means types are based only on their members.

So, if two object types have the same members then they are equivalent ↓ Image
Function type compatibility

When assigning one function to another, TypeScript checks that the function parameter types and the return type are compatible.

The parameter names aren’t important, it is only the types of parameters and number of parameters that are checked ↓ Image
Generic Types
Generic type allows us to create a specific type by allowing us to pass types into it as parameters.

For example, you can create an array of numbers, strings, booleans depending on the type you passed in <>.

Let's first discuss some in-built generic types.
Array<ItemType>: to create arrays that contain a certain type of elements. Image
Promise<ReturnType>: Specify the return type of asynchronous code. Image
Readonly<Type>: The Readonly type simply adds the readonly keyword to each of the object properties that are passed into it. Image
Partial<Type>: The Partial type makes all the members of the type
passed into it optional. Image
Record<KeyType, ValueType>: The Record type allows a
key-value object type to be created. Image
Generic Types become quite powerful when used with functions and interfaces.
Let's discuss how to create generic types in the following tweets.
Creating Generic Functions

Consider the following function in the image.

It takes an array and returns its first member, if the array is empty it returns null.

What if we want the same function to work with any other array types like int? Image
We can use the "any" type for it but then we won't have any knowledge of the return type.

Also, we can't keep adding types to a union.

The solution here is to create a generic function.
A Generic Function can take different types of parameters.

We define the type parameters for a generic function in angle brackets before the function’s parentheses.

The type parameters are placeholders for the real types.
We can choose any names we want for these. Image
Creating Generic Interfaces

We can similarly create a generic interface by passing types into an interface that are used within its definition. Image
A common use case for a generic interface is a generic form interface.

This is because all forms have values but the specific fields differ from form to form. Image
Creating generic classes

We can also create generic classes.
The following image shows the syntax and an example ↓ Image
That completes our final thread on the TypeScript guide.

If you have any doubts or questions, drop them below. We'll be happy to answer.
Follow @ScalerTopics for more such threads, tips, and tons of articles on all things Computer Science.

#TypeScript

β€’ β€’ β€’

Missing some Tweet in this thread? You can try to force a refresh
γ€€

Keep Current with Scaler Topics

Scaler Topics Profile picture

Stay in touch and get notified when new unrolls are available from this author!

Read all threads

This Thread may be Removed Anytime!

PDF

Twitter may remove this content at anytime! Save it as PDF for later use!

Try unrolling a thread yourself!

how to unroll video
  1. Follow @ThreadReaderApp to mention us!

  2. From a Twitter thread mention us with a keyword "unroll"
@threadreaderapp unroll

Practice here first or read more on our help page!

More from @ScalerTopics

Mar 15
Complete TypeScript Guide | Creating New Types

Your guide to migrating from JavaScript to TypeScript.

πŸ’™πŸ’™πŸ–€ (Part 2 of 3 Mega 🧡 on TS) Image
Here’s Part 1 of the series for those who missed or want to review πŸ‘‡

We can create a strongly typed array using the <type> or type[] syntax.

TS can also infer array types if not assigned explicitly.

If you try to add a different type element in the array, TS will raise an error ↓ Image
Read 14 tweets
Mar 8
Complete TypeScript Guide | The Basics

Your guide to migrating from JavaScript to TypeScript.

πŸ’™πŸ–€πŸ–€ (Part 1 of 3 Mega 🧡 on TS) Image
What is TypeScript?

TypeScript is a superset of JavaScript.

It has all the features of JS + more awesome features that make working with JavaScript easier resulting in lower stress levels.

TS = TypeScript
JS = JavaScript
TS majorly adds the following benefits to JS:

πŸ‘‰ Type System
πŸ‘‰ Code Refactoring
πŸ‘‰ Code Completion
Read 18 tweets

Did Thread Reader help you today?

Support us! We are indie developers!


This site is made by just two indie developers on a laptop doing marketing, support and development! Read more about the story.

Become a Premium Member ($3/month or $30/year) and get exclusive features!

Become Premium

Don't want to be a Premium member but still want to support us?

Make a small donation by buying us coffee ($5) or help with server cost ($10)

Donate via Paypal

Or Donate anonymously using crypto!

Ethereum

0xfe58350B80634f60Fa6Dc149a72b4DFbc17D341E copy

Bitcoin

3ATGMxNzCUFzxpMCHL5sWSt4DVtS8UqXpi copy

Thank you for your support!

Follow Us on Twitter!

:(