Alternative to decorate function in TypeScript

0 votes
Since TypeScript allows decorate only class, method, property or setter/getter because of hoisting, is there any way to decorate a raw function? Or maybe something similar to decoration mechanism or even write own custom mechanism to avoid TypeScript rules.
Jul 14, 2022 in TypeSript by Logan
• 2,140 points
570 views

No answer to this question. Be the first to respond.

Your answer

Your name to display (optional):
Privacy: Your email address will only be used for sending these notifications.
0 votes

Technically this is not one of the "TypeScript rules" but a problem with JavaScript. It is yet not syntactically possible to use @decorator for functions, but in its essence, a function decorator is just a higher-order function that wraps around the decorated function.

The closest thing that you can do is defining a function wrapper

function foo() {
    return "asd";
}

const wrap = (func: () => void) => {
    const wrapped = () => {
         console.log("wrapped");
         return func();
    }
    return wrapped;
}

console.log(wrap(foo)());
answered Jul 15, 2022 by Nina
• 3,060 points

edited 4 days ago

Related Questions In TypeSript

0 votes
1 answer

How to create ES6 Map in Typescript

Refer this as an example this.configs = new ...READ MORE

answered May 31, 2022 in TypeSript by Nina
• 3,060 points
1,003 views
0 votes
1 answer

How to declare and initialize a Dictionary in Typescript

Apparently this doesn't work when passing the ...READ MORE

answered May 31, 2022 in TypeSript by Nina
• 3,060 points
6,802 views
0 votes
1 answer

How to use moment.js library in angular 2 typescript app?

REFERENCE: https://momentjs.com/docs/#/use-it/typescript/ You can install this by using: npm install ...READ MORE

answered Jun 1, 2022 in TypeSript by Nina
• 3,060 points
1,864 views
0 votes
1 answer

Is it possible to extend types in Typescript?

you can intersect types: type TypeA = { ...READ MORE

answered Jun 7, 2022 in TypeSript by Nina
• 3,060 points
672 views
0 votes
1 answer

Where to find TypeScript version in Visual Studio?

If you only have TypeScript installed for ...READ MORE

answered Jun 7, 2022 in TypeSript by Nina
• 3,060 points
2,944 views
0 votes
1 answer

How to create enum type in TypeScript?

TypeScript 0.9+ has a specification for enums: enum ...READ MORE

answered Jun 8, 2022 in TypeSript by Nina
• 3,060 points
1,194 views
0 votes
1 answer

Cannot access web3 object with typescript and ethereum

You still need to instantiate it first. ...READ MORE

answered Sep 25, 2018 in Blockchain by slayer
• 29,370 points
2,978 views
0 votes
1 answer

How to apply zoom animation for each element of a list in angular?

Hey @Sid, do check if this link ...READ MORE

answered Jul 30, 2019 in Others by Vardhan
• 13,150 points
1,778 views
0 votes
1 answer
0 votes
1 answer

Can not bind to 'formgroup' since it is not a known property of 'form'

In order to rectify this error, you ...READ MORE

answered Feb 10, 2022 in Others by Rahul
• 9,680 points
21,968 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP