Excel sheets treats declared string as number

Viewed 941

I am using TypeScript with React.js. Even though I declared variables' type as string in my interface, when I upload Excel sheet, if there is a number, it treats it as number. All I need to treat it as string.

let data = XLSX.utils.sheet_to_json(ws, {
    header: headers,
    range: 1
}) as Array<Types.IExcelCourseRow>;

My IExcelCourseRow is:

{
    courseCode?: string;
    ...
}

Whenever my courseCode is 123, it treats is as 123. I need it to treat courseCode as '123'. What can I do?

2 Answers

This might be a small bug in sheet_to_json, keeping { raw: false } will resolve your problem.

here is the link for more discussion on this issue

Type validation in TypeScript only happens on compile time. So anything that happens on runtime, like (up)loading an excel file, will not help you out here. If you want to make sure that courseCode is a string, you'll have to type cast it yourself.

Related