Skip to content

Instantly share code, notes, and snippets.

@paszkowskiDamian
Last active February 16, 2021 11:17
Show Gist options
  • Save paszkowskiDamian/5016ed403982a6cd2fc93f4f86ba0b2c to your computer and use it in GitHub Desktop.
Save paszkowskiDamian/5016ed403982a6cd2fc93f4f86ba0b2c to your computer and use it in GitHub Desktop.
If you need to specify that some type is a key of some object where value is of type string you may use such construct.
interface Props<T extends Record<K, string>, K extends keyof T> {
data: T[];
row: ComponentType<T>;
primaryKey: K;
}
function List<T extends Record<K, string>, K extends keyof T>({
data,
row: Row,
primaryKey
}: Props<T, K>) {
return (
<ul>
{data.map((element) => (
<Row key={element[primaryKey]} {...element} />
))}
</ul>
);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment