Menu

I got a decorator to type check the generic of the class it's on and I'm way too happy about it

#typescript#reddittypescript#code#l340#class
Reading 0:00
15s threshold

In my framework, every handler has to write its route twice. Once in the decorator because the runtime needs the string, and once in the generic because the types need it.

@Route('ban') class Ban extends Handler<'kick'> {} // no errors. cool. love that for me

That compiles . The handler for ban now thinks it's handling kick and TS is completely fine with it.

And yes, I tried only writing it once. class Ban extends Route('ban') . It worked(?), but every handler read like mixin tomfoolery, and go-to-definition took you to a generated base class instead of the real one, and I just didn't like how it looked, so I went back to writing it twice.…

Continue reading — create a free account

Join HashtagPLUS to read full articles, follow hashtags, vote, and join the conversation.