laoqiren
laoqiren

Reputation: 3877

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:

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. So how to do with it?

supplement: The Options type declaration can't be got in application unless we declare it in our application. I mean to let the Options do like an object.

Upvotes: 0

Views: 64

Answers (2)

DJAMEL DAHMANE
DJAMEL DAHMANE

Reputation: 424

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);
            }
        }

Upvotes: 0

Jonas Wilms
Jonas Wilms

Reputation: 138257

Take the proper type here:

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

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

Upvotes: 1

Related Questions