sindresorhus / electron-store

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

`ElectronStore` type does not export `get`, `set`, or `delete` #276

Open raphaelmenges opened 5 months ago

raphaelmenges commented 5 months ago

Hi 👋,

I have upgraded the electron-store from 8.2.0 to 9.0.0. Thanks for moving to ESM!

Suddenly, Typescript complains that the methods get, set, or delete are not available for ElectronStore. I looked up the type definition and the methods are neither defined in 8.2.0 nor in 9.0.0. I am not sure why my typecheck did not complain before.

error TS2339: Property 'set' does not exist on type 'ElectronStore<any>'

Perhaps I am missing something in migrating from 8.2.0 to 9.0.0?

sindresorhus commented 5 months ago

Make sure you have the correct tsconfig.

sindresorhus commented 5 months ago

I looked up the type definition and the methods are neither defined in 8.2.0 nor in 9.0.0. I am not sure why my typecheck did not complain before.

The methods are inherited from Conf.

raphaelmenges commented 5 months ago

I have checked the electron-store module inside my node_modules directory. The type-fest dependency is contained within the electron-store module and the type definitions are found. The conf module however is installed on the same level as the electron-store module. I assume the conf module is required by multiple of my dependencies (perhaps one of your other nice modules!) and npm decided to install it on that level. I have not listed the conf in my package.json. When I change the path in the index.d.ts to conf of the electron-store module as follows, the type check in index.d.ts is fixed but the type check problem in my project persists:

import {type Except} from 'type-fest';
import Conf, {type Options as ConfigOptions} from '../conf/dist/source/types';

export {Schema} from '../conf/dist/source/types';

I am using over 1k (sub-)dependencies in the Electron project that just work fine how I setup the ESM environment, including native modules. Thus, I assume something with electron-store or conf might be broken. Perhaps you have an idea, otherwise I will try to make a minimal project that reproduces the issue!

tahafatih commented 5 months ago

same problem

s0hanian commented 5 months ago

I also have the same problem.

Sam-Spencer commented 5 months ago

I'm also having the same problem, and would also like to reiterate @raphaelmenges comment:

I am using over 1k (sub-)dependencies in the Electron project that just work fine how I setup the ESM environment, including native modules. Thus, I assume something with electron-store or conf might be broken.

frontierFlight commented 5 months ago

After using the latest version, I also encountered the same problem and had to use Anyscript!

sindresorhus commented 5 months ago

Can you confirm you have "type": "module" in your package.json and "module": "node16", "moduleResolution": "node16" in your tsconfig?


I tried adding this to an existing TypeScript project and it compiles fine for me (npm run build): https://github.com/sindresorhus/got/commit/40026f1acc81d8d78a8c66225bc22a5edbef68a2

raphaelmenges commented 5 months ago

Can you confirm you have "type": "module" in your package.json and "module": "node16", "moduleResolution": "node16" in your tsconfig?

I have "type": "module" in my package.json and "module": "esnext", "target": "esnext", "moduleResolution": "node" in my tsconfig. When changing the latter to "module": "node16", "moduleResolution": "node16", my own code does no more compile via Typescript due to problems with path resolving. I am using the following (popular?) template: https://github.com/cawa-93/vite-electron-builder/tree/main

PS: Using the mentioned template, I could reproduce the error in a minimal fashion: https://github.com/raphaelmenges/electron-store-type-error

sindresorhus commented 5 months ago

TypeScript does not treat your project as fully ESM unless you have moduleResolution set to either node16 or nodenext.

sindresorhus commented 5 months ago

I am using the following (popular?) template: cawa-93/vite-electron-builder@main

Then I suggest opening an issue there. That config is incorrect.

hywax commented 5 months ago

@sindresorhus hi, I added code that solves this error, please have a look at it https://github.com/sindresorhus/conf/pull/193

sindresorhus commented 5 months ago

Locking this as it seems clear the culprit is incorrect tsconfig.

Here is an example of a correct tsconfig: https://github.com/sindresorhus/tsconfig/blob/main/tsconfig.json