使用Interface在Typescript类型声明

时间:2018-04-03 13:23:59

标签: javascript typescript

我正在为一个框架编写类型声明,对于一个函数,它有一个类型为object的参数,但我想在声明类型中使用interface来描述它,如下所示: / p>

interface Options{
    template: string | Selector | AST;
    config (data: object): void;
}

interface Regular_Static {
    new (options: Options): Regular_Instance;
    // todo
}
declare var Regular: Regular_Static;

export = Regular

但是当我在我的应用程序中这样写时:

class HelloRegular extends Regular {
    constructor(options: object){
        super(options);
    }
}

它显示type object can't be assignment to type Options。那怎么办呢?

补充:除非我们在我们的应用程序中声明,否则无法在应用程序中获取Options类型声明。我的意思是让Options像对象一样。

2 个答案:

答案 0 :(得分:1)

在这里采取正确的类型:

interface Options{
  template: string | Selector | AST;
  config (data: object): void;
}

class HelloRegular extends Regular {
  constructor(options: Options){
    super(options);
  }
}

答案 1 :(得分:0)

您可以将该界面导入到您的类

      export interface Options{
             template: string | Selector | AST;
             config (data: object): void;
    }

      export interface Regular_Static {
           new (options: Options): Regular_Instance;
        // todo
                 }

并在此处导入该接口和类以使用它

    import { Options, Regular_Static} from 'yourfile.ts';
    class HelloRegular extends Regular {
          constructor(options: Options){
           super(options);
            }
        }