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

Restrict Paths for rename, remove and upsert #21

Open
anthonyjoeseph opened this issue Apr 7, 2022 · 0 comments
Open

Restrict Paths for rename, remove and upsert #21

anthonyjoeseph opened this issue Apr 7, 2022 · 0 comments
Labels
enhancement New feature or request

Comments

@anthonyjoeseph
Copy link
Owner

anthonyjoeseph commented Apr 7, 2022

🚀 Feature request

Current Behavior

rename, remove and upsert suggest all the same paths as get, set etc.

Desired Behavior

rename & remove:

  • should only suggest paths that end in an object's key (ones that don't end in '?' or sum-type-narrowing)

upsert:

  • should only suggest paths that end with a struct or record

Suggested Solution

This solution was proposed:

type UpsertableKeys<A> = Extract<
  keyof {
    [K in keyof A as true extends IsRecord<A[K]> ? K : never]: unknown;
  },
  string
>;

type ExtractChangeableKeys<A> = Extract<
  keyof {
    [K in keyof A as true extends IsNull<A[K]> ? never : Discriminant<A[K]> extends never ? K : never]: unknown;
  },
  string
>;

type Operation = "static" | "dynamic" | "upsert"

type _Paths<A, Op extends Operation, Acc extends string = never> = true extends IsRecord<A>
  ? _Paths<
      keyof A extends never ? unknown : BubbleUp<A>,
      Op,
      | Acc
      | (Op extends "static"
          ? Extract<keyof A, string>
          : Op extends "upsert"
          ? UpsertableKeys<A>
          : ExtractChangeableKeys<A>)
    >
  : Acc;

But it was determined that this caused deep type instantiation that adversely affected performance

Who does this impact? Who is this for?

users of rename, remove and upsert

Describe alternatives you've considered

Worst comes to worst, we could continue to emit those keys from Paths but give some other type of compile-time error

Additional context

Your environment

Software Version(s)
spectacles-ts 1.0.6
fp-ts 2.11
TypeScript 4.6
@anthonyjoeseph anthonyjoeseph added the enhancement New feature or request label Apr 7, 2022
@anthonyjoeseph anthonyjoeseph changed the title Restrict Paths for rename, remove and upsert (Must be Performant at Compile-Time) Restrict Paths for rename, remove and upsert Apr 7, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

1 participant