I'm not totally sure how to do this typing:
export interface EnrichedTableColumn<T> {
title: string;
rowKey: keyof T;
formatRow?: RowFormatter<The type defined in T for the key that is the rowKey here>; <- don't know how to do this
}
I want to say that the rowKey
will be a key of interface T.
So let's say T will be
interface TImplementation {
hello: string;
goodbye: number;
}
I would want rowKey to have to be either "hello" or "goodbye".
likewise, for formatRow, I would like to pass the type of that same key-value pair, i.e in the case of hello, formatRow would be RowFormatter<string>
.
I think I can do this with mapped types, but I'm not sure. Any help would be appreciated, I can clarify stuff if this is a bit confusing.
The use case is I would like to pass an array of these columns to a table, specifying only the interface which will define the data in the table.
so in the example, the table would have data that looks like this:
{
hello: string;
goodbye: number;
}[]
TS could then tell me if I make a mistake, for example, passing the following:
const COLS = [
{
title: "Hello",
rowKey: "hello",
formatRow: (value: number <- this is the mistake, should be string) => `Hello, ${value}!`
}
]