How can I define foo so that i don't have to specify as Constraint for each of the
array items ?
type Constraint = {
constraint: 'required' | 'equal'
}
function bar<K extends string>(d: Record<K, Constraint[]>) {
return Object.keys(d).reduce((a, b) => (a[b] = '', a),
{} as { [key: string]: string }) as Record<K, string>
}
const foo = { apple: [ { constraint: 'required' } ] }
// Error Type 'string' is not assignable to type '"required" | "equal"'.
const baz = bar(foo)
// Works fine
const foo2 = { apple: [ { constraint: 'required' } as Constraint ] }
const baz2 = bar(foo2)