0

I declare a object template which I populate with data:

   let dataTemplate = {
       publishedDate: "",
       Url: "",
       Title: "",
     }

     dataTemplate.publishedDate = xml.children[4].children[i].children[13].value;
     dataTemplate.Url = xml.children[4].children[i].children[16].value;
     dataTemplate.Title = xml.children[4].children[i].children[58].value;

I then declare a string array and try to push the above object to the array:

 let data: String[] = [];
 data.push(dataTemplate);
 

But I get a syntax error at "dataTemplate" at the following line:

data.push(dataTemplate);

[ts]
Argument of type '{ publishedDate: string; Url: string; Title: string; }' is not assignable to parameter of type 'String'.
  Property 'padStart' is missing in type '{ publishedDate: string; Url: string; Title: string; }'.

How come? I use TS 3.3 and cannot uppgrade (MS SPFx does not have support for newer versions.

1
  • 2
    why are you trying to push object to string array? String array can contain only strings. Commented Dec 17, 2020 at 14:56

1 Answer 1

1

/**
 * First of all, define type for templateData
 */
type Template = {
    publishedDate: string;
    Url: string;
    Title: string;
}
let dataTemplate: Template = {
    publishedDate: "",
    Url: "",
    Title: "",
}

/**
 * Wrong. because TS expects array of string, and you are going to use here array of Template
 */
let data: String[] = [];// not ok

// Ok
let data1: Template[] = [];

 data1.push(dataTemplate); // works as expected
Sign up to request clarification or add additional context in comments.

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.