Here it is example on playground. Playground
interface Product {
name: string,
id: number
productList?:ProductItem[]
}
interface ProductItem {
color: string,
size: number
}
type IValidation<T> = {
field: keyof T
nestedValidations?: IValidation<
Pick<
T,
{
[K in keyof T]-?: T[K] extends object ? K : never
}[keyof T]
>
>[] // THIS IS IMPORTANT FOR QUESTION!
validators?: (any | any | any)[]
}
export async function validateIt<T>(payload: T, validations: IValidation<T>[]): Promise<
Partial<{
[key in keyof T]: string[]
}>
> {
return Promise.resolve(payload);
}
const product: Product = {
id: 1,
name: 'playstation',
productList: [{
color: 'red',
size: 32
}
]
}
const test = validateIt<Product>(product, [
{
field: "productList",
validators: [],
nestedValidations: [
{
field: 'color',
validators: []
}
]
}
])
So clearly something is wrong with this part.
nestedValidations?: IValidation<
Pick<
T,
{
[K in keyof T]-?: T[K] extends object ? K : never
}[keyof T]
>
>[]
Any idea how to write type for this property which is array, i have tried many things so i am trying basically to use same type for payload and validator. And to reuse same type for nested property, which is basically element in array. So overall trying to use same type IValidation.
productList?:ProductItem[]