this question comes from How to push item to [string] in TypeScript
I want to create function like below.
declare var sqlitePlugin:any;
getItems(options, callback) {
var query: string = `SELECT item_code,
item_name
FROM items `;
var param: [string];
if (options['limit']) {
var limit = options['limit'];
query = query + " LIMIT ? ";
param.push(String(limit));
}
if (options['offset']) {
var offset = options['offset'];
query = query + " OFFSET ? ";
param.push(String(offset));
}
this.execQuery(query, param, (resultSet)=>{
this.items = [];
for(let i = 0; i < resultSet.rows.length; i++) {
var item: Item = new Item();
item.code = resultSet.rows.item(i).item_code;
item.name = resultSet.rows.item(i).item_name;
this.artists.push(item);
}
callback(this.items);
} );
}
execQuery function needs to be set 'param' parameter as type of [string].
So I declared 'param' tuple as [string].
I want to change param tuple dynamically by options['limit'] and options['offset'] value.
But param.push(String(limit)); fails because param is undefined so I tried var param: [string] = []; then syntax error occurred.
[ts] Type 'undefined[]' is not assignable to type '[string]'. Property '0' is missing in type 'undefined[]'.
How do I solve this problem?
Thanks in advance.