Property '0' is not in type

I have my interface like this

export interface Details {
    Name: [{
        First: string;
        Last: string;
    }];
}   

I have an observable configuration variable

Configuration: KnockoutObservable<Details> = ko.observable<Details>();

and I would like to assign it a value in the constructor as follows:

config = {
         Name: [{
               First: "ABC",
               Last: "DEF"
            },
            {
               First: "LMN",
               Last: "XYZ"
            }]
        };

this.Configuration(config);

and I get an error:

The types of the Name property are incompatible and the property 0 is not present in the type.

Enter '{First: string; Last: line; } [] 'is not assigned to the type' [{First: string; Last: line; }] '

I have no control over changing the interface, as it is used elsewhere. What is the correct way to initialize this configuration variable?

Thanks in advance.

+4
source share
4 answers

, :

    interface Details {
        Name: {
            First: string;
            Last: string;
        }[];
    }

, , , , .

+5

:

interface Details {
  Name: [{
    First: string;
    Last: string;
  }];
}

Name . . Typescript , . 1- Name , .

:

const config = {
  Name: [{
    First: "ABC",
    Last: "DEF"
  },
  {
    First: "LMN",
    Last: "XYZ"
  }]
};

, Name . , - 1-. , .

, , :

const config: Details = { Name: [{...}, {...}] };

, , , - :

if (names.length > 0) {
  const config = {
    Name: names as Details['Name']
  };
  Configuration(config);
}

( if, , , .)

: https://www.typescriptlang.org/docs/handbook/basic-types.html

+3

:

 interface Details {
        Name: Array<{
            First: string;
            Last: string;
        }>;
    }
0

Source: https://habr.com/ru/post/1678832/


All Articles