0

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.

4
  • 2
    If the constructor for Regular_Static expects an object with template and config why would the compiler allow you to call the constructor (as super(options)) with an object that may not have those properties ? Commented Apr 3, 2018 at 13:26
  • So what happens when you change options: object to options: Options? Commented Apr 3, 2018 at 13:26
  • @TitianCernicova-Dragomir There is no type Options in application Commented Apr 3, 2018 at 13:34
  • 1
    @laoqiren you define the type in the declarations, it's an interface, it does not need an implementation, so you can use it, as people have done in the answers :) Commented Apr 3, 2018 at 13:35

2 Answers 2

1

Take the proper type here:

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

class HelloRegular extends Regular {
  constructor(options: Options){
    super(options);
  }
}
Sign up to request clarification or add additional context in comments.

2 Comments

But the Options type declaration is not in the application and it is only at the declaration type file.
@laoqiren no problem as ts got duck typing.
0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.