Skip to content

dzhambulat/TypeScript-Generics

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

6 Commits
 
 

Repository files navigation

TypeScript-Generics

Bunch of exercises for practicing TypeScript generics

Function Generics

🔷 Factory function

Task

Create a generic factory function which has type parameter and returns an instance of this type.

💡Hint
✅ Solution
  function createInstance<T>(c: {new (): T}): T {
    return new c();
  }

🔷 Generic Defaults

Task

Create a generic functions with type parameter T which returns array of type T, but if no parameter passed return array of Number.

💡Hint

Use parameter defaults

✅ Solution

Conditional Generics

Task

Create a generic Type that takes a function type and returns its return type

💡Hint Check the type to extend the function type and use infer keyword
✅ Solution
type ReturnType1<T> = T extends (...args: any[])=> infer R ? R : never

About

Bunch of exercises for practicing TypeScript generics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors