sindresorhus / electron-store

Simple data persistence for your Electron app or module - Save and load user preferences, app state, cache, etc
MIT License
4.6k stars 150 forks source link

store.delete(key) doesn't type correctly in TypeScript #196

Open knownasilya opened 3 years ago

knownasilya commented 3 years ago
Screen Shot 2021-08-09 at 4 42 34 PM

I get this when I try to delete by key.

Argument of type '"mapgeo.login"' is not assignable to parameter of type 'keyof SyncStoreType'.ts(2345)
sindresorhus commented 3 years ago

Dot-notation paths will be supported strongly-typed when https://github.com/sindresorhus/dot-prop/commit/09adad91e5a5072014b3380e769be8aae89738f2 is released and then used in conf which this package depends on.

knownasilya commented 3 years ago

@sindresorhus Any specific reason that get supports them now, but not delete?

sindresorhus commented 3 years ago

get does not. It simply supports a string.

knownasilya commented 3 years ago

Hum, this looks like it does, but I might

Screen Shot 2021-08-09 at 5 22 02 PM

be mistaken.

knownasilya commented 3 years ago

@sindresorhus any thoughts?

sindresorhus commented 3 years ago

I don't have time to look into that. This will be properly resolved at some point like I mentioned before. As a temporary workaround, a string based overload could be added to .delete too.

airtonix commented 2 years ago

@knownasilya This is just typescript showing you the expanded types for the type case that match one of the 3 overloads.

image

🍊 is just your path... but it's a string nothing else. 🔵 is your Store type... shown expanded.

I suspect that @sindresorhus is going to rely on some dark sorcery revolving around turning a JsonSchema into typescript types instead of relying on some type like :

// helper types
type Length<T> = T extends { length: infer L } ? L : never;

type PopFront<T extends unknown[]> = T extends [infer U, ...any] ? U : never;
type Shift<T extends unknown[]> = T extends [any, ...infer R] ? R : never;

type Filter<T extends unknown[], U> = T extends []
  ? []
  : T extends [infer F, ...infer R]
  ? F extends U
    ? Filter<R, T>
    : [F, ...Filter<R, U>]
  : never;
type TupleIncludes<T extends unknown[], U> = Length<
  Filter<T, U>
> extends Length<T>
  ? false
  : true;
type StringIncludes<
  S extends string,
  D extends string
> = S extends `${string}${D}${string}` ? true : false;

type Includes<T extends unknown[] | string, U> = T extends unknown[]
  ? TupleIncludes<T, U>
  : T extends string
  ? U extends string
    ? StringIncludes<T, U>
    : never
  : never;
type Join<T extends unknown[], D extends string> = PopFront<T> extends string
  ? Length<T> extends 1
    ? `${PopFront<T>}`
    : Shift<T> extends string[]
    ? `${PopFront<T>}${D}${Join<Shift<T>, D>}`
    : never
  : never;

type Split<S extends string, D extends string> = string extends S
  ? string[]
  : S extends ''
  ? []
  : S extends `${infer T}${D}${infer U}`
  ? [T, ...Split<U, D>]
  : [S];

// Create an object type from `ObjectType`, where the keys
// represent the keys of the `ObjectType` and the values
// represent the values of the `ObjectType`
export type NestedKeyOf<ObjectType extends object> = {
  [Key in keyof ObjectType & (string | number)]: ObjectType[Key] extends object
    ? //@ts-ignore
      Key | `${Key}.${NestedKeyOf<ObjectType[Key]>}`
    : Key;
}[keyof ObjectType & (string | number)];

export type NestedType<T, P extends string> = Includes<P, '.'> extends true
  ? PopFront<Split<P, '.'>> extends keyof T
    ? NestedType<T[PopFront<Split<P, '.'>>], Join<Shift<Split<P, '.'>>, '.'>>
    : never
  : P extends keyof T
  ? T[P]
  : never;

TS Playground example here.

Which is naughty, because it ends up potentially breaking your computer due to inifinte recursion.

TreyOverton commented 1 year ago

Sep 7, 2021, @sindresorhus

This will be properly resolved at some point like I mentioned before.

Since this bug is still open in late May, 2023, do you have a timeline on when this might happen?

DarkGuy10 commented 10 months ago

late November 2023, still open

jhonbergmann commented 3 weeks ago

late September 2024, still open