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

Get returns any if the needle is incorrect #1

Closed
michaelKurowski opened this issue Aug 4, 2023 · 2 comments
Closed

Get returns any if the needle is incorrect #1

michaelKurowski opened this issue Aug 4, 2023 · 2 comments

Comments

@michaelKurowski
Copy link

michaelKurowski commented Aug 4, 2023

When the path passed to needle points to a place that does not exist on the type, it returns any. My expectation would be for it to return never or throw an error as otherwise it essentially ends up with a silent pass from the types point of view.

@geoffreytools
Copy link
Owner

Hi,

The return type when the needle is not found is unspecified, but yes it should be never.

I can make this work for most cases but I may fail to reliably reject bad free types because some built-in type combinations are structurally compatible.

Type checking the path in Get would most likely make it impossible to use Get when the Haystack is generic, because the type checker will not substitute the generic for its type constraint when performing checks, it's simply going to give up with a cryptic error. A workaround is to also accept an optional Model as I did with free-types/Match, but this API is quite frustrating to use to be honest because you always forget you have to use it and repeating the type constraint just feels bad.

I'm thinking that Lens could do the type checking because you are not likely to use it with generics: you create a Lens with an interface in mind and that could be the Model. When a Model is supplied, the type constraint for the query would try to apply Get<Query, Model> and if the call returns never it would return a union of valid paths to try instead.

@geoffreytools
Copy link
Owner

geoffreytools commented Aug 7, 2023

type-lenses now optionally perform type checking on Lens.

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

No branches or pull requests

2 participants