typescript-nullable icon indicating copy to clipboard operation
typescript-nullable copied to clipboard

Default to null and default to undefined

Open denistakeda opened this issue 6 years ago • 2 comments

Hello

Thank you for this awesome library, it's the best Maybe library for typescript I've ever seen. I discovered one issue though when introduced it to our codebase.

All nullable objects have type T | undefined | null, but many libraries use only one nullable values either null or undefined (all optional fields have type T | undefined).

Basically resolving this is quite straightforward, you just need default to some value, like withDefault(undefined, nullable) or withDefault(null, nullable). But use these all the time in the client code looks dirty. Maybe there can be some helpers in the library to resolve this library. Something like:

withDefaultUndefined<T>(nullable: Nullable<T>): T | undefined;
withDefaultNull<T>(nullable: Nullable<T>): T | null;

denistakeda avatar Jul 11 '18 08:07 denistakeda

I can create a PR myself if you ok with that

denistakeda avatar Jul 11 '18 08:07 denistakeda

Hello @denistakeda! Thanks for bringing this up. Feel free to open an PR. Some unit test coverage on the new functions would be great too if you don't mind 😀

kylecorbelli avatar Jul 15 '18 00:07 kylecorbelli