Rodger Hirthe

Rodger Hirthe

1598241360

TypeScript Generics Made Simple

Introduction to TypeScript Generics

#typescript #javascript #web-development #programming #developer

What is GEEK

Buddha Community

TypeScript Generics Made Simple
John David

John David

1630984612

How to Build Generic React Components with Typescript Generics

In this video tutorial, Santiago will show how to use Typescript Generics to build Generic React Components. Do you find yourself duplicating your code to do the same things multiple times? 
When building frontend applications, you may need to represent different entries in similar instances, for example aggregating search results within a single list. Separate typescript components are best to achieve this, but it can create problems such as code duplication which can slow down the process. Typescript generics provide an efficient solution to this problem by creating generic components.

⏱ TIMESTAMPS
0:00 - Intro
0:46 - Code Example
1:22 - Component Breakdown
3:58 - Using the Component

✔️ Santiago's code: https://codesandbox.io/s/typescript-generic-components-2zrus?file=/src/SelectList.tsx 

#typescript #generics #react 

Guide to Understanding Generics in Java

Introduction

Java is a type-safe programming language. Type safety ensures a layer of validity and robustness in a programming language. It is a key part of Java’s security to ensure that operations done on an object are only performed if the type of the object supports it.

Type safety dramatically reduces the number of programming errors that might occur during runtime, involving all kinds of errors linked to type mismatches. Instead, these types of errors are caught during compile-time which is much better than catching errors during runtime, allowing developers to have less unexpected and unplanned trips to the good old debugger.

Type safety is also interchangeably called strong typing.

Java Generics is a solution designed to reinforce the type safety that Java was designed to have. Generics allow types to be parameterized onto methods and classes and introduces a new layer of abstraction for formal parameters. This will be explained in detail later on.

There are many advantages of using generics in Java. Implementing generics into your code can greatly improve its overall quality by preventing unprecedented runtime errors involving data types and typecasting.

This guide will demonstrate the declaration, implementation, use-cases, and benefits of generics in Java.

#java #guide to understanding generics in java #generics #generics in java #guide to understanding generics in java

Rodger Hirthe

Rodger Hirthe

1598241360

TypeScript Generics Made Simple

Introduction to TypeScript Generics

#typescript #javascript #web-development #programming #developer

Verdie  Murray

Verdie Murray

1630958820

Why Generics? - JavaScript DataStructures and Algorithms in TypeScript

In this lesson we look at the key reason #generic type annotations are critical to maintainable type safe data structure creation in JavaScript / TypeScript

#JavaScript #typescript 

Christa  Stehr

Christa Stehr

1599322740

TypeScript Generics — The !Easy Way

Looking for a great (remote only) React Dev? Or just feel like having a chat? Visit my profile on LinkedIn and say hi! 😃

Image for post

Probably the most annoying part of my journey with TypeScript has been Generics. The syntax confused me from the start, and the concepts didn’t “naturally” make sense the way that everything else about TypeScript had up until that point.

So I did what every rational procrastinator would do, and tried to avoid the hell out of them… a tactic which failed miserably. Generics are EVERYWHERE, from commonly used libraries like Apollo, to little projects you need to jump onto and help out with, so the only way to deal with them is to learn them, practice using them, and essentially get over any aversion you have to them. _Brute force it — _there’s no other way.

NOTE: I won’t be dealing with Classes in this article, so please refer to the docs if you want to apply Generics to JS Classes.


Generics are like variables, except that instead of representing values, they represent types. E.g.

function getAge<T>(age: T): T {
  return age;
}

const myAge = getAge<number>(40);
console.log(myAge);    // logs 40

Ugly, isn’t it? If code readability is of utmost importance, then how is this an improvement? Well, your guess is as good as mine, so let’s go and drink the TypeScript Kool-Aid together… 😕.

So what’s going on in the code above? First, we declare a function that takes age as a parameter and simply returns it. The angle brackets you see in the beginning <T> means that this function takes a type of T. Because this is a variable, you can then go ahead and use it just like you would any other variable. So we use it to declare that age is of type T and then the function returns a value of type T.

Now, T could be any valid TypeScript basic type. Also, we could use any name or letter, like U or Name. We can even use lower case, like n, although the convention is to use an upper case letter or a word with an upper case first letter — e.g. Key.

We then go ahead and call the function getAge, and now we define what we want T to be — namely number. Finally, we log out myAge and then go and grab a cold beer and quietly cry into it after realizing that we are now officially “mid-life”… 😭🍺🍺🍺

#typescript #generics