Just starting out with Typescript and I've encountered an error stating that the split method does not exist on type number. I've tried narrowing down the type by checking the value's type, but so far it hasn't been successful. Below is the code snippet:
file.ts
interface AIObject {
CLARITY_CODE?: string | {};
CARAT?: number | {};
SHAPE_CODE?: string | {};
}
const numericKeys = [
"CARAT",
];
const arrObj: AIObject[] = [
{
CLARITY_CODE: "DE",
CARAT: 5,
SHAPE_CODE: "SI",
},
];
arrObj.map((obj: AIObject) => {
let ov = Object.keys(obj);
ov.forEach((val) => {
if (typeof obj[val as keyof AIObject] === "string") {
const check = obj[val as keyof AIObject]?.split("-");
// console.log(check);
if (check.length === 2) {
if (numericKeys.includes(val)) {
obj[val as keyof AIObject] = { $gte: check[1], $lte: check[0] };
} else {
obj[val as keyof AIObject] = [...check];
}
}
}
});
});