feat: Adding RequireProp type helper
Helpful type to require one property in a type. Could be used like
const changeEntries = dailyEntries.entries.filter(
(e): e is RequireProp<Entries, 'change'> => !!e.change,
)
...to allow accessing the entry.change property later without warning about Could be undefined.
Not sure about naming here. Could be RequiredProp (rather than require...). Or RequiredPick since we are picking one property.
Not seeing how this would be used, I say LGTM, but in my experience Kirill has opinions on being too precise with types, so I'll defer to him.
If it's an internal, one-off thing somewhere in our helpers ✅ But if other developers can face both versions of DailyEntries - one with optional and one with the required property -, then I'd probably create a new named interface/type. (Of course, we can create a new named interface/type using this helper too, which would be okay too.)
I like the RequiredProp name - from all the other options you mentioned.
I wonder about making it accept more than one key though, e.g. RequiredProps, or WithRequiredProps.
I was using it in a filter rule and then wanting to use the filtered data without checking again or using the non-null assertion operator !
Actually right now I don't need it anymore (change of solution), but I have used this previously and find it useful to avoid the !.
Merging this, as it would make some non-null assertions not needed in https://github.com/NaturalCycles/NCBackend3/pull/11426
:tada: This PR is included in version @naturalcycles/js-lib-v15.54.0 :tada:
The release is available on:
Your semantic-release bot :package::rocket: