Skip to content

Instantly share code, notes, and snippets.

@M-Zuber
Created February 21, 2018 10:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save M-Zuber/0d0d841762a8d72d9717da9690c635c0 to your computer and use it in GitHub Desktop.
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
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