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

Broken type helper (GetPgResourceRelations) #1755

Open
wesselvdv opened this issue Sep 22, 2023 · 4 comments · May be fixed by #1784
Open

Broken type helper (GetPgResourceRelations) #1755

wesselvdv opened this issue Sep 22, 2023 · 4 comments · May be fixed by #1784
Assignees
Labels

Comments

@wesselvdv
Copy link

wesselvdv commented Sep 22, 2023

Summary

Various type helpers appear to be broken (GetPgResourceRelations) for example always returns any even when supplying a fully typed PgResource with explicit relations. I would advice to use infer instead of property access to prevent type erasure.

Below works:

type GetRegistry<U> = U extends PgResource<any, any, any, any, infer R>
  ? R
  : never
type GetRelations<U> = U extends PgRegistry<any, any, infer R> ? R : never
type GetCodec<U> = U extends PgResource<any, infer C, any, any, any>
  ? C
  : never
type GetName<U> = U extends PgCodec<infer N, any, any, any, any, any, any>
  ? N
  : never
type GetPgResourceRelations<U> = U extends PgResource<
  any,
  any,
  any,
  any,
  any
>
  ? GetRelations<GetRegistry<U>>[GetName<GetCodec<U>>]
  : never

Whereas this doesnt:

type GetPgResourceRelations<TResource extends PgResource<any, any, any, any, any>> = TResource["registry"]["pgRelations"][TResource["codec"]["name"]];
@wesselvdv
Copy link
Author

Seems it's not entirely the fault of these type helpers. Seems I made some mistakes trying to make it fit into the existing PgRegistry which has type parameters that are narrowing too much.

@wesselvdv
Copy link
Author

Alright, looks like the issue is narrowing too much in the type parameters of PgRegistry. Causing typescript to get a union of "ExpliciitProperty" | string for relation properties which is the same as string.

@benjie
Copy link
Member

benjie commented Sep 28, 2023

I found when using inference that TypeScript would quickly resort to any's, potentially due to inference depth limits, but by using the [] syntax it would keep the types for a lot longer.

@benjie
Copy link
Member

benjie commented Sep 28, 2023

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
Status: 🥚 Platypus
2 participants