Sam
Sam

Reputation: 11

Type annotation for a complex type in Typescript

I have a piece of Javascript code, and I need to convert to Typescript, and since I am new to Typescript and I don't have much experience in this area, I have been struggling to figure it out so far. There is a variable ( options):

class Global {
    ...
    options: any;

and it is initialized elsewhere in the code:

global.options = extend(
        {
            pauseFocusSelector: '.pause-on-focus'
        },
        global.options);

So I decided to define an interface in Typescript to assign its type:

interface OptionType
{
    pauseFocusSelector: string[];
};

but when I compile it, I get this error:

Error TS2345: Argument of type 'OptionType' is not assignable to parameter of type 'string'.

Could anyone give me a hint to solve it?

Upvotes: 1

Views: 251

Answers (1)

Älskar
Älskar

Reputation: 2577

This is because you are attempting to assign a string to a property of an interface that has a string array type.

You either need to change your OptionType interface to be:

interface OptionType
{
  pauseFocusSelector: string;
};

Or if pauseFocusSelector needs to be string[] then you need to change your assignment line to be:

global.options = extend({
  pauseFocusSelector: ['.pause-on-focus']
}, global.options);

Upvotes: 1

Related Questions