feat: add NonNullable builtin type to remove null from type union - #1875
Conversation
|
looks like you forget loacally update deps |
Co-authored-by: Max Graey <maxgraey@gmail.com>
|
This will make aspect better! Thank you. |
Co-authored-by: Max Graey <maxgraey@gmail.com>
|
Okay went with your suggestion. Still think it's a bit of an over optimization, but I want to get this merged. |
|
The package-lock changes seem unrelated, can you unstage them before merging? :) |
|
Also, there seems to be some inconsistency with line endings at the end of new files. While probably not super important, it would be great to always have an |
|
@dcodeIO I added a new line to the end of the test |
| /** A special type evaluated to the return type of T if T is a callable function. */ | ||
| declare type returnof<T extends (...args: any) => any> = ReturnType<T>; | ||
| /** A special type that excludes null and undefined from T. */ | ||
| declare type NonNullable<T> = T extends null | undefined ? never : T; |
There was a problem hiding this comment.
Heh, toyed around with it a little and it works, I just don't understand how
There was a problem hiding this comment.
It's a conditional type. NonNullable<null> becomes the never type which would cause it to fail to typecheck. We don't have this issue because it's not possible to type something as null, see here
There was a problem hiding this comment.
My guess is that this somehow decomposes string | null (which extends null I guess?) to string and null, with the null turned into never, so that the outcome would be string | never with the never being dropped or something.
|
@dcodeIO I made a script that added new lines to source files in the tests folder that were missing them. |
|
Would have preferred a separate PR as noted earlier, but now that you already did it I guess we can just do it here |
|
Thanks! |
|
Got confused with your question. But glad it's merged! |
This adds the TS type mentioned by @MaxGraey and should fix #1874. Also did a little refactor for asserting single type argument.
Also I'm not sure why my change removed the element section in the wat files. I tried reverting the refactor and just having my single new function and it still generated the same wat files.