0

I'm trying to build a class that will contain constant values to be used at different places.

The structure should be something like this:

class JavascriptEvents {
    static change: string = "change";
    static click: string = "click";
    static blur: string = "blur";
    static inputChange: string = "input propertychange paste";
    static dblClick: string = "dblclick";
    static bootstrap: Object = {
        accordion: {
            show: "show.bs.collapse",
            shown: "shown.bs.collapse",
            hide: "hide.bs.collapse",
            hidden: "hidden.bs.collapse"
        },
        modal: {
            shown: "shown.bs.modal",
            show: "show.bs.modal",
            hide: "hide.bs.modal",
            hidden: "hidden.bs.modal",
            loaded: "loaded.bs.modal"
        }
    }
}

Question: How should the bootstrap part be nested so I can reference an event like:

$("someElement").on(JavascriptEvents.bootstrap.modal.shown, function(){
    // do whatever needed
});

1 Answer 1

1

I think the problem is the : Object declaration, which hid all the type information from the call site. If you simply remove it, the type checker should be happy again.

E.g.:

class JavascriptEvents {
    static bootstrap: Object = {
        modal: {
            shown: "shown.bs.modal",
        }
    }
    static bootstrap2 = {
        modal: {
            shown: "shown.bs.modal",
        }
    }
}

let jq: any;
jq.on(JavascriptEvents.bootstrap.modal.shown); // Error
jq.on(JavascriptEvents.bootstrap2.modal.shown); // Works

Playground

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.