Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add NonNullable builtin type to remove null from type union #1875

Merged
merged 19 commits into from Jun 8, 2021

Conversation

willemneal
Copy link
Contributor

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.

@willemneal willemneal changed the title feat: add NonNullable builtin type to remove null from T feat: add NonNullable builtin type to remove null from type union May 29, 2021
@MaxGraey
Copy link
Member

looks like you forget loacally update deps

src/resolver.ts Outdated Show resolved Hide resolved
Willem Wyndham and others added 2 commits May 29, 2021 11:18
@willemneal willemneal requested a review from MaxGraey May 29, 2021 15:48
@jtenner
Copy link
Contributor

jtenner commented May 29, 2021

This will make aspect better! Thank you.

@MaxGraey MaxGraey requested a review from dcodeIO June 1, 2021 15:08
@willemneal willemneal requested a review from MaxGraey June 1, 2021 20:47
@willemneal
Copy link
Contributor Author

@MaxGraey @dcodeIO anything else?

@willemneal willemneal requested a review from MaxGraey June 7, 2021 13:35
src/resolver.ts Outdated Show resolved Hide resolved
src/resolver.ts Outdated Show resolved Hide resolved
Willem Wyndham and others added 2 commits June 7, 2021 09:50
Co-authored-by: Max Graey <maxgraey@gmail.com>
@willemneal willemneal requested a review from MaxGraey June 7, 2021 14:30
src/resolver.ts Outdated Show resolved Hide resolved
src/resolver.ts Outdated Show resolved Hide resolved
@willemneal
Copy link
Contributor Author

Okay went with your suggestion. Still think it's a bit of an over optimization, but I want to get this merged.

src/common.ts Outdated Show resolved Hide resolved
@willemneal willemneal requested a review from dcodeIO June 7, 2021 17:10
@dcodeIO
Copy link
Member

dcodeIO commented Jun 7, 2021

The package-lock changes seem unrelated, can you unstage them before merging? :)

tests/compiler/nullable.ts Outdated Show resolved Hide resolved
@dcodeIO
Copy link
Member

dcodeIO commented Jun 7, 2021

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 \n at the end I think :)

@willemneal
Copy link
Contributor Author

@dcodeIO I added a new line to the end of the test .ts file, but all of the json files seem to all not have a \n at the end of the file.

@willemneal willemneal requested a review from dcodeIO June 7, 2021 18:30
@@ -1314,6 +1314,10 @@ declare type valueof<T extends unknown[]> = T[0];
declare type ReturnType<T extends (...args: any) => any> = T extends (...args: any) => infer R ? R : any;
/** 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;
Copy link
Member

Choose a reason for hiding this comment

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

Wait, how does this work?

Copy link
Member

Choose a reason for hiding this comment

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

Heh, toyed around with it a little and it works, I just don't understand how

Copy link
Contributor Author

@willemneal willemneal Jun 8, 2021

Choose a reason for hiding this comment

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

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

Copy link
Member

Choose a reason for hiding this comment

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

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.

@willemneal willemneal requested a review from dcodeIO June 8, 2021 17:32
@willemneal
Copy link
Contributor Author

@dcodeIO I made a script that added new lines to source files in the tests folder that were missing them.

@dcodeIO
Copy link
Member

dcodeIO commented Jun 8, 2021

Would have preferred a separate PR as noted earlier, but now that you already did it I guess we can just do it here

@dcodeIO dcodeIO merged commit 42883cb into AssemblyScript:main Jun 8, 2021
@dcodeIO
Copy link
Member

dcodeIO commented Jun 8, 2021

Thanks!

@willemneal
Copy link
Contributor Author

Got confused with your question. But glad it's merged!

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.

[Feature Request] Built in to make a type non-nullable
4 participants