2021年5月2日星期日

"Infinite" amount of generic types

Is there any TypeScript solution, that would help me have an "infinite" amount of generic types, without having to type all of them, of course?

Example bellow:

type Args<T extends React.ComponentType<any>> = { component: T, presetProps: React.ComponentProps<T>};    function buildComponents<T extends React.ComponentType<any>>(...args: Args<T>[] ) : any    function componentJ({name, age}: {name: string, age: number}){    return <h1>123</h1>  }    function componentI({ age}: { age: number}){    return <h1>123</h1>  }    buildComponents({component:componentJ, presetProps:{name: "Jon", age: 12}},                   {component: componentI, presetProps:{age: 44}})  

With this code, I got an error on {component: componentI, presetProps:{age: 44}}, because it misses the name, but I wanted this to get the type from the componentI and not from the the componentJ, which was the case.

Thanks in advance for all the help.

https://stackoverflow.com/questions/67361427/infinite-amount-of-generic-types May 03, 2021 at 05:53AM

没有评论:

发表评论