it's shorter, delegates type checking of optionals to the type Optionals<T>. I suggest you try the code to understand it. it needs typescript 3.8 (worth upgrading for ?. and ?? operators ).
Thank you! Since I'm new to the TS stuff, your solution looks a bit gobbledy-gookish to me at first, so I'll definitely look at it carefully to make sure that I truly grok it before putting it in. But this looks very promising!
And we are already using TS 3.8.3 - so ? and ?? operators shouldn't be a problem.
Witch means, return me an object type that has the same properties as Props, but with property types equal to the property name if the underlying property is not required .
then :
that means return me a union of all the optional properties names of Props.
Then the idea is to have setDefaults(), not compile if you try to pass default parameters not declared in Props, or if you forgot to add defaults that where declared optional in Props.
This is where you will thank typescript for having your back everytime you forgot to handle a default parameter, because typescript will catch it.
Excellent stuff. And thank you(!) for the extended explanation. I've been coding for 20+ years and I've done plenty of generics in languages like C#. But when you're in a "JavaScript mindset", it can still take a little bit to get your head back in that mindspace.
All though I'm generally a big fan of generics, when you haven't been working with them for awhile, code like that above can look like the first time that you dove into regular expressions.
Cheers!
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.
Hi Adam,
You may want to try this :
it's shorter, delegates type checking of optionals to the type
Optionals<T>
. I suggest you try the code to understand it. it needs typescript 3.8 (worth upgrading for?.
and??
operators ).Thank you! Since I'm new to the TS stuff, your solution looks a bit gobbledy-gookish to me at first, so I'll definitely look at it carefully to make sure that I truly grok it before putting it in. But this looks very promising!
And we are already using TS 3.8.3 - so
?
and??
operators shouldn't be a problem.Cheers!
Yes, the hardest part is this one :
Witch means, return me an object type that has the same properties as Props, but with property types equal to the property name if the underlying property is not required .
then :
that means return me a union of all the optional properties names of Props.
Then the idea is to have setDefaults(), not compile if you try to pass default parameters not declared in Props, or if you forgot to add defaults that where declared optional in Props.
This is where you will thank typescript for having your back everytime you forgot to handle a default parameter, because typescript will catch it.
Excellent stuff. And thank you(!) for the extended explanation. I've been coding for 20+ years and I've done plenty of generics in languages like C#. But when you're in a "JavaScript mindset", it can still take a little bit to get your head back in that mindspace.
All though I'm generally a big fan of generics, when you haven't been working with them for awhile, code like that above can look like the first time that you dove into regular expressions.
Cheers!