Im trying(and failing) to build table generator based on row and column arrays.
Im stuck at defining TS types.
Column prop ‘field’ values should constrain row record properties.
For example:
columns = [{field: 'field1'}, {field:'field2'}];
should require for me to send records like:
records = [{field1: 'anyValue', field2: 'anyValue'}]
Looks like im failing at sending columns as exact value, so generic have no idea what to do with it. My best take is below:
type Columns = readonly { label: string; field: string }[]
type Row<C extends Columns> = Record<C[number]['field'], any>
const columns= [
{
label: 'Name',
field: 'name',
},
{
label: 'Nip',
field: 'nip',
},
] as const
const rows = [{name:'asd',nip:'asd'},{name:'ghj',nip:'ghj'}];
function renderTable<const C extends Columns>(columns:C ,row: Row<C>) { ... }
renderTable(columns,rows);
/**
* TS error on 32:"rows"
*
* Type '{ name: string; nip: string; }[]' is missing the following properties from type
* 'Row<readonly [{ readonly label: "Name"; readonly field: "name"; }, { readonly label:
* "Nip"; readonly field: "nip"; }]>': name, nip
*/
Could someone bo so kind to show me the right way of handling problems like those ?