Created
May 14, 2019 19:53
-
-
Save christianwish/1efe45699ee2d1eda19d33db0fedd90e to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
enum DocStatus { | |
OPEN = "OPEN", | |
WON = "WON", | |
DRAFT = "DRAFT", | |
OVERDUE = "OVERDUE", | |
CANCELED = "CANCELED", | |
}; | |
interface Doc { | |
readonly id: string, | |
readonly [propName: string]: any; | |
}; | |
interface DocFn { | |
<T extends Doc>(x: T, y?: any, z?: any): T; | |
}; | |
interface Invoice extends Doc { | |
readonly invoiceNumber: string, | |
readonly status: DocStatus, | |
readonly date?: string, | |
readonly clientId?: string, | |
}; | |
const invoices: Invoice[] = [ | |
{ id: "1", invoiceNumber: "", status: DocStatus.OPEN }, | |
{ id: "2", invoiceNumber: "", status: DocStatus.DRAFT }, | |
{ id: "3", invoiceNumber: "", status: DocStatus.DRAFT }, | |
{ id: "4", invoiceNumber: "", status: DocStatus.WON }, | |
]; | |
const makeStatusChange:Function = (x: DocStatus, y: DocStatus) => <T extends Doc>(c: T) => { | |
return { | |
...c, | |
status: (c.status === x) ? y : c.status, | |
}; | |
}; | |
const openToCanceled:<T extends Doc>(c: T) => T = makeStatusChange(DocStatus.OPEN, DocStatus.CANCELED); | |
const newInvoices:Invoice[] = invoices.map(openToCanceled); | |
console.log(newInvoices); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment