Created
February 21, 2018 10:01
-
-
Save M-Zuber/0d0d841762a8d72d9717da9690c635c0 to your computer and use it in GitHub Desktop.
Test case to understand how to use typescript generics properly. needs noImplicitAny set
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
interface FilterOption { | |
key: string | number; | |
displayValue: string; | |
predicate: <TEntity>(e: TEntity) => boolean; | |
} | |
interface Registration { | |
id: number; | |
name: string; | |
date: string; | |
} | |
function foo(registrations: Registration[]):FilterOption[] { | |
return registrations.map(r => ({ | |
key: r.id, | |
displayValue: r.name, | |
predicate: (r: Registration) => r.date.length <= 8 | |
})); | |
} | |
const registrations = [{ | |
id: 1, | |
name: 'max', | |
date: '2018212' | |
}, { | |
id: 12, | |
name: 'george', | |
date: '20182110' | |
}, { | |
id: 123, | |
name: 'Sam', | |
date: '201821201' | |
}, { | |
id: 1234, | |
name: 'froddo', | |
date: '2018212' | |
}]; | |
const options = foo(registrations); | |
console.log(options); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment