Skip to content

Instantly share code, notes, and snippets.

@danywalls
Created August 10, 2022 08:01
Show Gist options
  • Save danywalls/19aba5c23ee5ee5158b98d63a770d2d4 to your computer and use it in GitHub Desktop.
Save danywalls/19aba5c23ee5ee5158b98d63a770d2d4 to your computer and use it in GitHub Desktop.
class Invoice {
public amount: number;
public description: string;
country: string;
}
class SalesInvoices extends Invoice {
products: Array<string> = [];
}
class PurchaseInvoice extends Invoice {
tax: number;
}
const invoicesToProcess: Array<Invoice> = [];
function createInvoice(
description: string | number,
amount: number,
country
): Invoice {
const descriptionValue =
typeof description === "number" ? `${description}` : description;
return {
description: descriptionValue,
amount,
country,
};
}
const invoiceWithNumber = createInvoice(1231321, 120, "ES");
const invoIceWithString = createInvoice("Bananana", 90, "USA");
console.log(invoIceWithString, invoiceWithNumber);
console.clear();
const salesInvoice = new SalesInvoices();
salesInvoice.amount = 130;
salesInvoice.country = "ES";
const basicInvoice = new Invoice();
basicInvoice.amount = 90;
basicInvoice.country = "USA";
function addInvoiceToProcess(invoice: Invoice) {
if (invoice instanceof SalesInvoices) {
invoicesToProcess.push(invoice);
}
}
addInvoiceToProcess(basicInvoice);
addInvoiceToProcess(salesInvoice);
console.log(invoicesToProcess);
console.clear();
interface ValidInvoice {}
function isValidInvoice(invoice: Invoice): invoice is SalesInvoices {
return invoice.amount > 100 && invoice.country === "ES";
}
invoicesToProcess.forEach((invoice) => {
if (isValidInvoice(invoice)) {
console.log(invoice);
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment