Is the typeguard not enough? invoiceData.defaultTax in myObj also doesn't work (found on StackOverflow)
export const TaxTypes = ["AL", "BC", "NB", "NL", "NS", "ON", "EXEMPT"] as const;
export type TaxType = (typeof TaxTypes)[number];
export const TaxRate: Record<TaxType, number> = {
AL: 0.05,
BC: 0.05,
NB: 0.05,
NL: 0.15,
NS: 0.15,
ON: 0.13,
EXEMPT: 0,
};
type Invoice = {
defaultTax: TaxType;
};
const invoiceData: Invoice = { defaultTax: "ON" };
const myObj: Partial<Record<TaxType, number>> = {};
for (const taxType of TaxTypes) {
if (taxType === "AL") {
myObj[taxType] = 100;
console.log("Object", myObj);
}
}
const testOne = invoiceData.defaultTax + 50;
console.log(testOne);
if (myObj[invoiceData.defaultTax] !== undefined) {
const testTwo = myObj[invoiceData.defaultTax] + 25;
console.log(testOne, testTwo);
}