call signatures, polymorphism, overloading, generics

hams·2023년 2월 8일
0

typescript

목록 보기
1/5
post-thumbnail

1. call signature

TypeScript call signatures are used to declare the expected parameters and return type of a function. They are used to provide type information for the function and ensure that the function is being called with the correct arguments.

A call signature can be declared in a type, interface or class.
type Add = (a: number, b: number) => number

type Add = {
(a: number, b: number) : number
}

const add: Add = (a, b) => a+b

2. Overloading

TypeScript supports function overloading, which allows a function to have multiple implementations based on the number or type of arguments passed to it. This is done by defining multiple function signatures with the same name, but with different parameters. The TypeScript compiler chooses the correct implementation based on the arguments passed to the function at runtime.

function add(a: number, b: number): number;
function add(a: string, b: string): string;
function add(a: any, b: any): any {
  return a + b;
}

In this example, the add function has two overloads: one for adding numbers, and another for concatenating strings. The implementation of the function simply returns the sum of its arguments, which will be the correct behavior for both overloads.

3. polymorphism

Polymorphism in TypeScript refers to the ability of a single function or method to work with objects of different types. It enables a function to dynamically adapt to the data it receives, allowing for more reusable and flexible code.

There are two main forms of polymorphism in TypeScript:

Overloading: This allows a single function to have multiple signatures, each of which takes different arguments and returns different values. The correct implementation is chosen based on the types of arguments passed to the function.

Interface Polymorphism: This is a form of polymorphism that is based on interfaces. An interface defines a set of properties and methods that an object must implement. A class can implement multiple interfaces, allowing it to be treated as an object of multiple types.

Polymorphism is an important concept in object-oriented programming and is commonly used in TypeScript for creating reusable and flexible code.

4. generic

Generics in TypeScript allow you to write functions and classes that can work with a variety of data types, rather than being tied to a specific type. This allows for greater flexibility and reuse of code.

A generic function or class is declared with a placeholder type, represented by angle brackets (). This placeholder type can be any valid TypeScript type, and it can be replaced by an actual data type when the function or class is called.

For example, consider a generic function that takes an array of values and returns the first element of that array:

function firstElement<T>(arr: T[]): T {
  return arr[0];
}

This function can be used with arrays of any data type:

let numbers = [1, 2, 3];
let firstNumber = firstElement(numbers);
console.log(firstNumber); // outputs 1

let strings = ['hello', 'world'];
let firstString = firstElement(strings);
console.log(firstString); // outputs 'hello'

Generics can also be used with classes, enabling them to work with a variety of data types:

class GenericClass<T> {
  value: T;
  setValue(value: T): void {
    this.value = value;
  }
}

let numberClass = new GenericClass<number>();
numberClass.setValue(10);
console.log(numberClass.value); // outputs 10

let stringClass = new GenericClass<string>();
stringClass.setValue('hello');
console.log(stringClass.value); // outputs 'hello'

Generics are a powerful tool in TypeScript, allowing you to write code that is more flexible and reusable.

0개의 댓글