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

  • SEARCH
  • Home
  • 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 9225371
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T04:33:35+00:00 2026-06-18T04:33:35+00:00

I am just wondering if in TypeScript you can define custom events on your

  • 0

I am just wondering if in TypeScript you can define custom events on your classes or interfaces?

What would this look like?

  • 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-18T04:33:36+00:00Added an answer on June 18, 2026 at 4:33 am

    How about this simplified event to be used as a property? Stronger typing of the owning class and no inheritance requirement:

    interface ILiteEvent<T> {
        on(handler: { (data?: T): void }) : void;
        off(handler: { (data?: T): void }) : void;
    }
    
    class LiteEvent<T> implements ILiteEvent<T> {
        private handlers: { (data?: T): void; }[] = [];
    
        public on(handler: { (data?: T): void }) : void {
            this.handlers.push(handler);
        }
    
        public off(handler: { (data?: T): void }) : void {
            this.handlers = this.handlers.filter(h => h !== handler);
        }
    
        public trigger(data?: T) {
            this.handlers.slice(0).forEach(h => h(data));
        }
    
        public expose() : ILiteEvent<T> {
            return this;
        }
    }
    

    used like so:

    class Security {
        private readonly onLogin = new LiteEvent<string>();
        private readonly onLogout = new LiteEvent<void>();
    
        public get LoggedIn() { return this.onLogin.expose(); } 
        public get LoggedOut() { return this.onLogout.expose(); }
    
        // ... onLogin.trigger('bob');
    }
    
    function Init() {
        var security = new Security();
    
        var loggedOut = () => { /* ... */ }
    
        security.LoggedIn.on((username?) => { /* ... */ });
        security.LoggedOut.on(loggedOut);
    
        // ...
    
        security.LoggedOut.off(loggedOut);
    }
    

    Improvements?

    A gist for this

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Just wondering if anyone can optimize this usortMonths() function to be better? Basically I
Just wondering if this can be enforced by the database or not... I have
Just wondering what code I would need to do this?
Just wondering is it possible to build CLI app that can be run from
I was having a look at the JS code generated by TypeScript on this
Just wondering if there are any typical issues when using this form of database
Just wondering how I can fix following error when trying to build Apache CXF
Just wondering if we can update an already built xml file using xml serialization/deserializtion
Just wondering if .NET provides a clean way to do this: int64 x =
Just wondering, if there is any way or codes, that can set countdown timer

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.