How can I create an object based on an interface file definition in TypeScript?

后端 未结 11 1426
[愿得一人]
[愿得一人] 2020-12-04 05:01

I have defined an interface like this:

interface IModal {
    content: string;
    form: string;
    href: string;
    $form: JQuery;
    $message: JQuery;
          


        
11条回答
  •  盖世英雄少女心
    2020-12-04 05:50

    If you want an empty object of an interface, you can do just:

    var modal = {};
    

    The advantage of using interfaces in lieu of classes for structuring data is that if you don't have any methods on the class, it will show in compiled JS as an empty method. Example:

    class TestClass {
        a: number;
        b: string;
        c: boolean;
    }
    

    compiles into

    var TestClass = (function () {
        function TestClass() {
        }
        return TestClass;
    })();
    

    which carries no value. Interfaces, on the other hand, don't show up in JS at all while still providing the benefits of data structuring and type checking.

提交回复
热议问题