Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8780139
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T19:58:06+00:00 2026-06-13T19:58:06+00:00

Is there any way to hook into the declaration process of classes in TypeScript?

  • 0

Is there any way to hook into the declaration process of classes in TypeScript?

e.g. consider the following class

class Foo extends Bar
{
      constructor() { super(); }
}

I currently apply aspects to this type by calling my weaver manually like so:

weaver.applyAspects(Foo);

Meaning that I modify the behavior of some of the methods on the class.
I would rather see that this happens automagically just by inheriting in the above case the “Bar” super class.

Doing some funky stuff in the constructor seems wrong since that would apply to every new instance, not just the prototype itself.

I guess I could overwrite the __extends function (which is used in the generated javascript output) , but that seems a bit funky too.

Any other alternatives?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T19:58:07+00:00Added an answer on June 13, 2026 at 7:58 pm

    If you want to use AOP in TypeScript, you can use an existing AOP framework by supplying a definition for it. Here is a full example using jQuery.aop.

    Using AOP in this manner won’t affect any of your existing definitions as the AOP code is invisible on the code you are cross-cutting.

    aop.d.ts

    declare class Advice {
        unweave(): void;
    }
    
    declare interface PointCut {
        target: Object;
        method: string;
    }
    
    declare class Aop {
        before: (pointCut: PointCut, advice: Function) => Advice[];
        after: (pointCut: PointCut, advice: Function) => Advice[];
        afterThrow: (pointCut: PointCut, advice: Function) => Advice[];
        afterFinally: (pointCut: PointCut, advice: Function) => Advice[];
        around: (pointCut: PointCut, advice: Function) => Advice[];
        introduction: (pointCut: PointCut, advice: Function) => Advice[];
    }
    
    interface JQueryStatic {
        aop: Aop;
    }
    

    app.ts

    /// <reference path="jquery.d.ts" />
    /// <reference path="aop.d.ts" />
    
    class ExampleClass {
        exampleMethod() {
            alert('Hello');
        }
    }
    
    jQuery.aop.before(
        { target: ExampleClass, method: 'exampleMethod' },
        function () { alert('Before exampleMethod'); }
    );
    
    jQuery.aop.after(
        { target: ExampleClass, method: 'exampleMethod' },
        function () { alert('After exampleMethod'); }
    );
    
    var example = new ExampleClass();
    example.exampleMethod();
    

    Example source: AOP with TypeScript.

    Update

    To add the same concerns to a class and all compatible classes, you can’t re-use the base class points. This is because the wrapper is the original base-class function wrapped in the points, which will be the wrong implementation for the class that extends the base class.

    The only time this would be valid is if your function only called super() in which case it would work anyway.

    Here is how I would add the same concerns to many classes – the AopHelper would only need to exist once in your program:

    /// <reference path="jquery.d.ts" />
    /// <reference path="aop.d.ts" />
    
    class ExampleClass {
        exampleMethod() {
            alert('Hello');
        }
    }
    
    class SecondClass extends ExampleClass {
        exampleMethod() {
            alert('World');
        }
    }
    
    class AopHelper {
        static weave(targets: Object[], method: string, point: Function, advice: Function) {
            for (var i = 0; i < targets.length; i++) {
                point({ target: targets[i], method: method }, advice );
            }
        }
    }
    
    var classes: any[] = [ExampleClass, SecondClass];
    
    AopHelper.weave(classes, 'exampleMethod', jQuery.aop.before, () => { alert('Before exampleMethod'); });
    AopHelper.weave(classes, 'exampleMethod', jQuery.aop.after, () => { alert('After exampleMethod'); });
    
    var example = new SecondClass();
    example.exampleMethod();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

is there any way to hook a debug function into a hook such as
Is there any possible way to add swing into a shutdown hook (that is,
Using Roslyn's Workspaces, is there any way to acquire a hook into the active
Is there any way to hook into an old VB6 program using C# and
Is there any way in which I can automatically convert a Custom Class Object
Is there a way to hook into the internals of CompositionContainer? For example, let's
is there any way I can install a system-wide ShellExecute hook using C++ without
Is there a way to hook into MVC3 unobtrusive remote validation on the client,
Is there anyway that I can hook into the tab completion for bash from
Is there any way in Notepad++ (or even with another tool) to change the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.