Another "challenge" with the approach you've outlined here is that the requiredString and requiredNumber values only exist under the props object - but optionalString, optionalBoolean, and optionalNumber exist as standalone variables. As you've pointed out, you can get them back into one object, but you'd have to manually add requiredString and requiredNumber to that object as well.
That's not insurmountable, but I gotta play with it for a bit to see if there's a slicker way of handling that...
exportconstMyTSComponent:React.FC<Props>=({optionalString="default",optionalBoolean=true,optionalNumber=42,...args})=>{constprops={optionalString,optionalBoolean,optionalNumber,...args};console.log(props);props.optionalString.split("");return(<>
Here is MyComponent:
<br/>{props.children}</>);};
whit this you would get a fully typed props object that you could use as you used to
I've actually built a helper function now that takes the existing props and a simple object that defines any default values and then returns it mapped to the Props type. I'll probably outline that in a near-future post.
Thanks for taking the time to point me along!
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
Another "challenge" with the approach you've outlined here is that the
requiredString
andrequiredNumber
values only exist under theprops
object - butoptionalString
,optionalBoolean
, andoptionalNumber
exist as standalone variables. As you've pointed out, you can get them back into one object, but you'd have to manually addrequiredString
andrequiredNumber
to that object as well.That's not insurmountable, but I gotta play with it for a bit to see if there's a slicker way of handling that...
You could try something around these lines
whit this you would get a fully typed props object that you could use as you used to
I've actually built a helper function now that takes the existing
props
and a simple object that defines any default values and then returns it mapped to theProps
type. I'll probably outline that in a near-future post.Thanks for taking the time to point me along!