简体   繁体   English

使用接口在Typescript类型声明中表达对象

[英]using Interface to express object in Typescript types declaration

I'm writing types declaration for a framework, for a function, it has a parameter whose type is object , but I want to describe it using interface in the declaration type like this: 我正在为框架写类型声明,对于一个函数,它有一个类型为object的参数,但是我想使用声明类型中的interface来描述它:

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

But when I write like this in my application: 但是当我在应用程序中这样写时:

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

it shows that type object can't be assignment to type Options . 它表明type object can't be assignment to type Options So how to do with it? 那么如何处理呢?

supplement: The Options type declaration can't be got in application unless we declare it in our application. 补充: Options类型声明不能在应用程序中获取,除非我们在应用程序中声明它。 I mean to let the Options do like an object. 我的意思是让Options像对象一样。

Take the proper type here: 在此处输入正确的类型:

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

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

you can import that interface to you class like that 您可以像这样将接口导入您的班级

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

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

and here import that interface and class to use it 在这里导入该接口和类以使用它

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM