🕵️ SicherheitslückenWhat continuous operational resilience looks like under DORA(09.09.2026 um 17:53 Uhr)
🔧 AI Nachrichten OpenAI seeks tougher AI rules. CIOs may feel the ripple effects(10.09.2026 um 12:11 Uhr)
🔧 AI Nachrichten Mistral valued at €21bn after €3bn Series D funding round(08.09.2026 um 10:19 Uhr)
🪟 Windows TippsWindows XP's Cursor Indicator Is Getting a Windows 11 Refresh(25.08.2026 um 13:00 Uhr)
🕵️ SicherheitslückenWhat continuous operational resilience looks like under DORA(09.09.2026 um 17:53 Uhr)
🔧 AI Nachrichten OpenAI seeks tougher AI rules. CIOs may feel the ripple effects(10.09.2026 um 12:11 Uhr)
🔧 AI Nachrichten Mistral valued at €21bn after €3bn Series D funding round(08.09.2026 um 10:19 Uhr)
🪟 Windows TippsWindows XP's Cursor Indicator Is Getting a Windows 11 Refresh(25.08.2026 um 13:00 Uhr)

🔧 Programmierung 🕛 vor 2 Jahren 2 Min Lesezeit
0

Generics in TypeScript

↗ Quelle (dev.to)
🗣️ Stimme:

Generics in TypeScript allow you to write functions and classes that can work with a variety of data types while maintaining type safety. They provide a way to create reusable and type-safe components, offering a level of abstraction that promotes code flexibility.



Let's start by looking at a simple example. Consider a function that returns the input it receives:




CODE
const num = 1;
const str = 'a';
function returnValue(val: any): any {
return val
};

const numReturn = returnValue(num);
const strReturn = returnStr(str);






While this works, it lacks type safety. The any type means that the function can accept any type of argument, but it doesn't provide information about the type of the returned value. This is where generics come into play.



Generics allow us to create functions and classes that can work with different data types while preserving type information. Let's rewrite the returnValue function using generics:




CODE
function returnValue<T>(val: T): T {
return val
};

const numReturn = returnValue(21)






In this example, the T inside the angle brackets represents a type parameter. The type parameter T acts as a placeholder for the actual type that will be provided when the function is called. In the case of returnValue(21), T is inferred to be number (If you pass a string it will be taken as a string).



Benefits:




  • Type Safety: With generics, you can write code that is both flexible and type-safe. The TypeScript compiler can catch type-related errors at compile time, providing a safety net that plain JavaScript lacks.


  • Code Reusability: Generics allow you to create functions and classes that can work with different types without sacrificing reusability. This can lead to more modular and maintainable code.


  • Functionality Abstraction: Generics enable the creation of highly abstracted and versatile components. You can write code that doesn't depend on specific types, making it adaptable to a wide range of scenarios.


  • Improved Readability: By using generics, your code becomes more expressive and readable. Type parameters provide meaningful names for the types involved, making the code easier to understand.

    Conclusion:




In this introductory guide, we've explored the basics of generics in TypeScript. As you delve deeper into TypeScript development, generics will become an essential tool in your toolkit for writing robust, reusable, and type-safe code. By leveraging generics, you can enhance the flexibility and maintainability of your TypeScript projects, making them more scalable and easier to work with in the long run.

Vollständiger Original-Bericht
Ausführliche Details, Code-Beispiele & Hersteller-Stellungnahme auf dev.to.
↗ Original-Artikel auf dev.to lesen
Wie bewertest du diesen Beitrag?
1 Klick Feedback
Teilen mit Netzwerk & Team:

Community-Analysen & Experten-Meinungen 0

Verfasse deine eigene Analyse, teile Workarounds oder diskutiere diesen Vorfall im Blog.
Noch keine Community-Analyse verfasst. Markiere einen Textabschnitt oder klicke oben auf Eigene Analyse verfassen“!
Community Pulse: Relevanz-Einschätzung
1 Klick Experten-Votum
🔴 Akute Relevanz 0%
🟡 In Evaluierung 0%
🟢 Keine Auswirkung 0%
Spannende Innovation 0%
Verwandte Story-Cluster & Quellen (Vektor-KI)
Port 8095 Engine
1 Quelle
Sam Altman calls GPT-6 Astra rollout ‘messy’ as enterprise users wait for access
1 Quelle
Swiss government explores replacing Microsoft 365 with open-source software
1 Quelle
What continuous operational resilience looks like under DORA
Ähnliche Beiträge
🔍 Verwandte News

Auch interessante Nachrichten Generics in TypeScript

Thematisch verwandte Begriffe: Generics, TypeScript · 6 Treffer

Laden...

Videos werden geladen ...

Laden...

Beiträge werden geladen ...

Laden...

Videos werden geladen ...

Laden...

Beiträge werden geladen ...

Laden...

Videos werden geladen ...