Skip to content

Changed the Array.flat() definition to work with Typescript Compiler #1275

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

Merged
merged 3 commits into from
May 16, 2020

Conversation

torch2424
Copy link
Contributor

@torch2424 torch2424 commented May 15, 2020

Hello!

So when I was upgrading as-bind, I noticed that AssemblyScript wasn't working with the Typescript compiler, due to a bad definition the Typescript compiler didn't like:

Screenshot from 2020-05-15 12-26-51

So I went ahead and just modified my node_modules directly. I tried the type: valueof<T extends unknown[]>[], but that didn't work. I think this new type in this PR correctly represents what we want, and it works (ignore the red test, was just trying to test things really quick):

Screenshot from 2020-05-15 12-52-11
Screenshot from 2020-05-15 12-51-57
Screenshot from 2020-05-15 12-51-51

cc @dcodeIO @MaxGraey

We may want to push a minor out to npm for this? Since this will require people to modify their node modules for the tsc compatibility.

@MaxGraey
Copy link
Member

MaxGraey commented May 15, 2020

I guess for definitions better use something like this:

flat<U>(this: Array<Array<U>>): U[];

@torch2424
Copy link
Contributor Author

torch2424 commented May 15, 2020

Made requested changes, thanks @MaxGraey 😄 👍

@dcodeIO
Copy link
Member

dcodeIO commented May 15, 2020

Interestingly, this appears to work as well

flat(): T

while a conditional type never yields never ¯\(ツ)/¯

flat(): T extends unknown[] ? valueof<T>[] : never

Edit: Now it suddenly does :)

@jtenner
Copy link
Contributor

jtenner commented May 15, 2020

I definitely wrote this type. Whoops! I'll remember this little trick for later.

@dcodeIO
Copy link
Member

dcodeIO commented May 15, 2020

@MaxGraey Wdyt of

flat(): T extends unknown[] ? valueof<T>[] : never

to avoid the U and reuse valueof<T>?

@jtenner
Copy link
Contributor

jtenner commented May 16, 2020

This is just @dcodeIO 's suggestion, removing the valueof expression which resolves the exact same type, unless I am mistaken.

flat(): T extends unknown[] ? T : never;

@MaxGraey
Copy link
Member

MaxGraey commented May 16, 2020

I guess it should be:

flat(): T extends unknown[] ? typeof T[0] : this;

@dcodeIO
Copy link
Member

dcodeIO commented May 16, 2020

Compilation will currently fail if T is not an array. Returning this in this case might be an alternative option, but looks like it'd change the return type, which we cannot currently represent.

@dcodeIO dcodeIO merged commit 2b4dce0 into AssemblyScript:master May 16, 2020
@torch2424 torch2424 deleted the tsc-array-flat branch May 18, 2020 04:22
@torch2424
Copy link
Contributor Author

Thanks for updating my PR @dcodeIO ! 😄 👍 Glad we got this in haha! 😂

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