What is Generic Programming?
- Generic programming is a style of computer programming in which algorithms are written in terms of types to-be-specified-later that are then instantiated when needed for specific types provides as parameter.
Type parameter?
- Type parameter like 'T' in TypeScript, are a fundamental aspect of generic programming, a programming style where algorithms are written in terms of types to-be-specified-later. These parameters allow you to rite flexible, reusable functions and clases that work with any type.
Basics of Type Parameters
1. Generic Functions and classes : Type parameters are used in the definition of generic functions, classes, or interfaces. They act as placeholders for the types that are passed in when the function is called or the clas is instantiated.
2. Syntax : In typescript, type parameteres are typically named with single uppercase letters like 'T','U','V', but they can have more desriptibe names. They are defined using angle brackets('<>')
Why use type parameters
1. Type safety: enable you to write code with better type safety. The typescript compiler can enforce correct usage of the types passed to generic functions or classes.
2. Reusabillity : They increase code reusabillity. You can write a function or a class that works with any type, avoiding code duplication for different types.
3. Flexible Yet Strict : Generics provice a way to use types flexibly while sill maintaining strict type check. The actual type for a type paramter is determined at the time of invocation or instantiation.
'개발' 카테고리의 다른 글
0. HTTP란? (1) | 2024.07.28 |
---|---|
Typescript Custom Decorator (0) | 2024.02.03 |
Typescript Type narrowing (0) | 2023.11.12 |
Static and non-static(instance) methods (1) | 2023.11.12 |
NestJs - Mircoservices (2) | 2023.11.08 |