What you need to know about Decorators

preview_player
Показать описание
I share my opinion on decorators in Typescript/Javascript and what you need to know about them.


----
#benawad
Рекомендации по теме
Комментарии
Автор

Decorators are heavily used in angular, nestjs, typeOrm, .... It really gives a very user friendly structure for complex applications.
The problem that i see with it can be exemplified by what i thought the first time i saw it used:
What is happening? How are things working this way? Is this still Javascript/Typescript or is this magic?
I felt like i was loosing control in the way that i wanted to express myself in creating an app. This is probably one of the reasons why most react devs don't opt for angular...

One good thing that i like about decorators:
Apps are getting to be more and more complex everyday and giving devs some power with a rich metadata system will probably be the future of app developing, although it may not be yet a path for the present.
And if one thinks about it, every frameworks and library are already empowering us by striping control from us.

estranhokonsta
Автор

Decorators should just by syntactic sugar for a higher-order function, as seen in other languages such as Python, instead of inventing an entirely new system. This also would mean package writers would not have to explicitly support decorators, rather it would be up to the package user to decide if they want to use them or not.

MaxPicAxe
Автор

Coming from a Java background where many libraries use annotations, I tried out dependency injection in Typescript using Tsyringe and it surprised me. Pretty simple and easy to use. Thought there’d be more issues getting DI to work in TS. Definitely not necessary, but was helpful for injecting services, loggers, and managing singletons.

Scottydawgs
Автор

A good example of Decorators is Flutter . They use @required to describe a required fields/args for the function/class.

qbamc
Автор

finally good explanation on decorators <3

iUsroftEWZL
Автор

Hey Ben, can you elaborate more on why you think decorators are an anti pattern? Thanks, Helge

helgewieding
Автор

typeGraphql vs nexus? which is good for production?
.

amol_
Автор

@autobind for binding functions in class components?

franzekan
Автор

Very nice, never used decorators. Is GraphQL worth using?

aleksd
Автор

watched a few other videos on this topic and wasn't able to understand. But i was able to understand yours.

i notice that the Is_logged_in is a common decorator on routes.

snakeb
Автор

why do js/ts complicate simple stuff? I mean decorators in python are so simple to understand but in js/ts it's just confusing and weird

uniqueo-gh
Автор

Thank you Ben for another amazing video. Have learnt so much from all of them.

I have one question that is not related to this video, but is VS code and Typescript and hope you might be able to help.

When I import a component, is there anyway of listing all the props that the component can accept using Intellisense? I get the error saying that required props are missing, but when I hover over the component tag, I'd like to see a list of props, without having to make them all required.

davec
Автор

I use decorators in Django and Python :)

aashayamballi
Автор

I'm learning typescript for the first time because of loopback 4 and omg are these decorators horrible to use...

weltmeister
Автор



Use case for it in backend when using nodejs with typescript and express you dont really want to set a router folder and add a bunch of router.get, router.post, router.put and so on because itll hard to maintain a lot of routes this happens at work our backend ruby on rails has a wack implementation.


its really nice to implement a Get route like:


function Get(routeName: string) { ...handle route here}.




then you can just use it on a method like:


Get('users')
getAllUsers() {}


hehehe but I guess its a preference also. nice video ben im still waiting for the podcast. :)

kid_kulafu_
Автор

Thank so much, i followed from this video

baihocmoi
Автор

Decorators only became popular in Java because it didn't have function composition / functions as variables, and now TS/JS try to implement this workaround just to appeal to Java devs =/
I only see decorators being useful for timings or other debug purposes. I think it would be better if they couldn't modify the decorated function, only wrap restricted (stateless/pure?) code around it.

FunctionGermany
Автор

So basically they're nice to use but tedious to make. Got it.

bernardoquina
Автор

Been watching your content but gotta say, I disagree with your opinion on decorators. Their great for auth, generic responses and scalable across multiple endpoints

alifyamohamedali
Автор

I only use decorators in python and I guess Apollo ...

japrogramer