How can I create an empty string array in Typescript?

I would like to have an array containing string error messages. Here is the code I came up with:

var errors: [string];
errors = [];
Object.keys(response.data.modelState).forEach(function (key) {
    errors.push.apply(errors, response.data.modelState[key]);
});

I tried several different ways to add typescript definition to variable errors, but none of them work for this case. The first definition works fine, but then when I click the values, I need to click on the array, and when I set:

errors = []; 

Then it gives me an error message:

Severity Code Description TS2322 project file line error Type 'undefined []' is not assigned to type '[string]'. The '0' property is missing in the type undefined [] '. Severity Code Description Project file line Build error: Type 'undefined []' is not assigned to type '[string]'.

+4
1

:

// instead of this
// var errors: [string];
// we need this
var errors: string[];
errors = [];

:

...forEach(function (key) {...

, , , /

Object.keys(response.data.modelState)
      .forEach(function (value, key) {
    errors.push.apply(errors, response.data.modelState[key]);
});

, , this

Object.keys(response.data.modelState)
      .forEach( (value, key) => {
    errors.push.apply(errors, response.data.modelState[key]);
});
+7

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


All Articles