Skip to content

Conversation

@fiddur
Copy link

@fiddur fiddur commented Oct 6, 2025

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.

@fiddur
Copy link
Author

fiddur commented Oct 6, 2025

Not sure about naming here. Could be RequiredProp (rather than require...). Or RequiredPick since we are picking one property.

@mrnagydavid
Copy link
Collaborator

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.

@fiddur
Copy link
Author

fiddur commented Oct 7, 2025

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 !

@fiddur
Copy link
Author

fiddur commented Oct 7, 2025

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 !.

Copy link
Member

@kirillgroshkov kirillgroshkov left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't mind it.
RequiredProp name SGTM.

I also don't mind not shipping it until it's needed.
Both fine by me.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants