Skip to content

Conversation

@aleclarson
Copy link
Contributor

@aleclarson aleclarson commented Oct 21, 2019

The current types were broken in TS 3.6+ so I partially rewrote them.

And fromDepth was misspelled depthFrom.

@coveralls
Copy link

Coverage Status

Coverage remained the same at 100.0% when pulling 9963c59 on aleclarson:patch-1 into 04b45e7 on blakeembrey:master.

@blakeembrey blakeembrey merged commit c36c46c into blakeembrey:master Nov 15, 2019
: Element
: never
tuple: InferElement<Array<T[number]>>
}[Array<T[number]> extends T ? 'array' : 'tuple']
Copy link
Owner

Choose a reason for hiding this comment

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

What was the reasoning behind this switch between tuple and array behavior? I wasn't able to replicate the reason this was here when testing different solutions.

Copy link
Owner

Choose a reason for hiding this comment

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

3.7 included recursive types, but it didn't improve for within conditions - I was still getting the same error when testing this out. It's the main reason I still need to do:

{
  [K in Extract<keyof T, number>]: PickValue<T[K]>;
}[number]

As long as it wasn't any sort of explicit tuple support, that's fine - I made sure I have tests for tuples on master but wasn't sure if this did something I failed to test.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Did you intentionally exclude the arguments support?

T extends IArguments
  ? unknown
  : // ...

Copy link
Owner

Choose a reason for hiding this comment

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

@aleclarson Not intentionally, it wasn't clear that line was there to just make it be the unknown type instead of any. I'm not sure how useful it is to make it unknown now that I think about it, but there's definitely a test for arguments on master that passes.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

unknown forces you to use type guards or an explicit typecast, so less runtime errors

Copy link
Owner

Choose a reason for hiding this comment

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

I’m aware of how it works. To clarify, I don’t see the point of changing a type on someone in this library if they’re using something unsafe - otherwise we could change all any types. I’d hope arguments usage is an edge case in TS, and by putting it in this library it’s condoning the usage arguments as the input type - which I believe it shouldn’t be used as for performance reasons.

@blakeembrey
Copy link
Owner

I believe I was able to simplify this a bit (after my first attempt using 3.7 recursive type features failed on ReadonlyArray:

export type PickValue<T> = T extends ReadonlyArray<any>
  ? {
      [K in Extract<keyof T, number>]: PickValue<T[K]>;
    }[number]
  : T;

export type FlatArray<T extends ArrayLike<any>> = Array<PickValue<T[number]>>;

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.

3 participants