How TypeScript Generics Help You Write Less Code
Text Source:
#CODE #WRITE #LESS #YOU #HELP #GENERICS #TYPESCRIPT #HOW
How TypeScript Generics Help You Write Less Code
When developers discuss Typescript, one of their main complaints is that you have to write more code to achieve the same result. While I agree with that, I think Typescript Generics can reduce the code you need to write to a great extent.
TS Generics can be used to bring abstraction into your TS interfaces. Using generics, you can pass an interface as a param to another interface.
Instead of writing an interface for every response and adding these keys, you can simply use Generics to create something like this:
We can create a similar function for POST requests which also takes JSON as params of type B and the server will send back a JSON response of type R:
Similarly, there can be a PATCH request function as well, which handles partial updates of any entity.
Typescript has the potential to increase the developer experience tenfold if appropriately configured. These are some strategies to make that configuration process more comfortable. I hope this will help you use Typescript better in your current codebase.
Learn to code for free. freeCodeCamp's open source curriculum has helped more than 40,000 people get jobs as
Видео How TypeScript Generics Help You Write Less Code автора TypeScript и наука
Видео How TypeScript Generics Help You Write Less Code автора TypeScript и наука
Информация
30 ноября 2023 г. 0:02:54
00:01:19
Похожие видео