I need to push the result object in an array. But typescript is showing an error
TSError: тип Unable to compile TypeScript:
test.ts:15:10 - error TS2454: Variable 'x' is used before being assigned.
15 return x;
~
This is my code. I don’t know to declare a global variable in typescript
import * as fs from "fs";
import * as path from "path";
import * as csv from "fast-csv";
function search_words(word: string) {
var x: object[];
fs.createReadStream(path.resolve(__dirname, ".", "data", "data.csv"))
.pipe(csv.parse({ headers: true }))
.on("error", (error) => console.error(error))
.on("data", (row) => {
if (row.english_word === word) x.push(row);
})
.on("end", (rowCount: number) => console.log(`Parsed ${rowCount} rows`));
return x;
}
console.log(search_words("Hello"));
This method is I know.any other method is possible
let hi; // global variable
function hello{
hi = "hi mom"
}