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

后端 未结 11 1400
[愿得一人]
[愿得一人] 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 06:00

    Here another solution what i am using frequently. However I am not sure is good practice or not, please comment below if not.

    /// Interface
    export default interface BookInterface {
      title: string,
      author: string,
      id: any
    }
    
    /// Creating Class
    export class BookClass implements BookInterface {
      title: string;
      author: string;
      id: any;
    
      constructor(title: string, author: string, id: any) {
        this.title = title;
        this.author = author;
        this.id = id;
      }
    }
    
    /// How to use it
    let book: BookInterface = new BookClass(title, author, id);
    

    Thanks :)

提交回复
热议问题