Hello, I am currently attempting to assign the correct type to an object with nested values.
Here is a link to the code in the sandbox: https://codesandbox.io/s/0tftsf
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 CRITICAL FOR MY 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: []
}
]
}
])
I am encountering a type error and trying to determine the correct type for the nestedValidations property, which should align with the interface Product.