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

Add built-in indexof/valueof types #671

Merged
merged 2 commits into from Jun 19, 2019
Merged

Add built-in indexof/valueof types #671

merged 2 commits into from Jun 19, 2019

Conversation

dcodeIO
Copy link
Member

@dcodeIO dcodeIO commented Jun 15, 2019

This PR investigates a special type valueof<T> that obtains the value type of an array, a set, a map or a class with an indexed get overload (like typed arrays).

Might help to implement things like #516 but the implementation is actually a bit short-sighted. TypeScript supports resolving types of the form T[0] for example, that does exactly this, but the resolver isn't yet powerful enough to support this (resolving of any expression etc.). So I guess the question is: Does it make sense to add this now, and once the resolver has been reworked, simply keep supporting it for backwards compatibility?

@MaxGraey
Copy link
Member

Nice. What about keyof?
Btw idiomatic way to retrieve value in TS:

type ValueOf<T> = T[keyof T];

@dcodeIO
Copy link
Member Author

dcodeIO commented Jun 15, 2019

That's what I meant with a bit short-sighted. A proper implementation would first need a new type node kind keyof Something, then a new type node kind T[Something] and finally the resolver must be able to resolve that. Not a 100% sure, but since the resolver requires a rework anyway (resolve any expression etc.), it looks like doing this properly would depend on doing that first. Really more a hack in this form, but maybe a useful one so we can get TypedArray#set out the door.

@MaxGraey
Copy link
Member

I see. So as intermediate solution this definitely make sense

@jtenner
Copy link
Contributor

jtenner commented Jun 16, 2019

Count me in! This enables many great comparison patterns for my testing software.

@dcodeIO
Copy link
Member Author

dcodeIO commented Jun 17, 2019

Thinking about this a little more, I figured that having our own special types might actually better represent what these actually do, because in TS, keyof doesn't just get the indexed access key, but all possible keys

type TheType = keyof Array<i32>; // number | "length" | "fill" ...

which is most likely nothing we'd support anytime soon anyway. Now, sadly, keyof is a keyword and we can't have a keyof<T> type, but we could just name ours indexof<T> obtaining the index type and valueof<T> obtaining the value type of indexed access. Wdyt?

@jtenner
Copy link
Contributor

jtenner commented Jun 17, 2019

It would be awesome to have a type declaration statement in the function body.

type Child = indexof<T>; // etc. 

Just my 2 cents. At first glance, I think this might cause problems with the resolver. I would be grateful for the type itself without the statement :) .

@dcodeIO
Copy link
Member Author

dcodeIO commented Jun 17, 2019

This now adds indexed access overloads for Map and Set to unify/simplify the concept by binding it to [] overloads specifically, and introduces the corresponding indexof<T> type.

@dcodeIO dcodeIO changed the title Add built-in valueof type Add built-in indexof/valueof types Jun 17, 2019
@dcodeIO dcodeIO merged commit c99becc into master Jun 19, 2019
@dcodeIO
Copy link
Member Author

dcodeIO commented Jun 21, 2019

@dcodeIO dcodeIO deleted the valueof-type branch September 20, 2019 09:06
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.

None yet

3 participants