type-fest icon indicating copy to clipboard operation
type-fest copied to clipboard

Proposal: `Must`

Open LucienLee opened this issue 4 years ago • 8 comments

Like Required make all properties in T required.

I want to propose adding Must, which makes all properties in T NonNullable.

type Must<T> = {
  [P in keyof T]-?: NonNullable<T[P]>;
};

If it makes sense, I can make a PR.

LucienLee avatar Jun 26 '20 18:06 LucienLee

What's the use-case and how does it differ from Required?

sindresorhus avatar Jun 27 '20 13:06 sindresorhus

Required only make the property not optional, but keeping null. I think that we need a way to strip null too.

See related discussion here

LucienLee avatar Jun 28 '20 07:06 LucienLee

Ah, that makes sense. I think this could be useful. However, I don't think the name is clear enough.

How about NonNullableDeep?

sindresorhus avatar Jun 28 '20 16:06 sindresorhus

Interesting, something that makes the value:

  • Required
  • Not null
  • Not undefined

I would suggest StrictlyRequired, what do you think?

papb avatar Jun 28 '20 18:06 papb

Is this type supossed to nest within object properties? If so, then a Deep suffix is valid, because thats the naming pattern used throughout the project. Since the type would deeply apply NonNullable I think NonNullableDeep is the name that makes the underlying logic obvious.

kainiedziela avatar Jun 29 '20 11:06 kainiedziela

@kainiedziela You're very right, the Deep suffix is very valid! Let me change my suggestion to StrictlyRequiredDeep.

I don't think NonNullableDeep is the best because it seems to imply that the only thing happening is NonNullable being applied recursively. I'd even prefer RequiredAndNonNullableDeep or NonNullableAndRequiredDeep over NonNullableDeep, but StrictlyRequiredDeep seems to fit it well and is shorter.

papb avatar Jun 29 '20 14:06 papb

I didn't think that applying NonNullable recursively at initial, but it's a good idea too. So how do you guys think about adding two types:

  1. A single level NonNullable types, name as StrictlyRequired or something else
  2. A recursively deep one, name as NonNullableDeep or StrictlyRequiredDeep

LucienLee avatar Jul 07 '20 16:07 LucienLee

@LucienLee That looks great to me.

papb avatar Jul 07 '20 17:07 papb

need NonNullableDeep, current version is not recursive.

image

tjx666 avatar Dec 20 '22 06:12 tjx666