admin管理员组

文章数量:1296489

I'm using electron-store for a demo electron project. In my main.ts process, I'm attempting to initialize the store and retrieve a simple settings object. It looks like this:

// main.ts
import Store, { Schema } from 'electron-store';

type settingsType = {
  settings: {
    environment: 'development' | 'test' | 'prod',
    refreshInterval: number,
  }
}

const schema: Schema<settingsType> = {
  settings: {
    type: 'object',
    properties: {
      environment: {
        type: 'string',
        enum: ['development', 'test', 'prod'],
        default: 'development',
      },
      refreshInterval: {
        type: 'number',
        minimum: 10000, // ten seconds
        maximum: 600000, // ten min
        default: 60000, // one min
      }
    },
    default: {},
    required: ['environment', 'refreshInterval'],
  }
}

const store = new Store<settingsType>({schema});

// ERROR: TS2339: Property 'get' does not exist on type 'ElectronStore<settingsType>'.
let settingsStore: settingsType = store.get('settings');

I think I'm defining everything as expected, but I'm still getting a TS error from the store.get call. What am I missing?

I'm using electron-store for a demo electron project. In my main.ts process, I'm attempting to initialize the store and retrieve a simple settings object. It looks like this:

// main.ts
import Store, { Schema } from 'electron-store';

type settingsType = {
  settings: {
    environment: 'development' | 'test' | 'prod',
    refreshInterval: number,
  }
}

const schema: Schema<settingsType> = {
  settings: {
    type: 'object',
    properties: {
      environment: {
        type: 'string',
        enum: ['development', 'test', 'prod'],
        default: 'development',
      },
      refreshInterval: {
        type: 'number',
        minimum: 10000, // ten seconds
        maximum: 600000, // ten min
        default: 60000, // one min
      }
    },
    default: {},
    required: ['environment', 'refreshInterval'],
  }
}

const store = new Store<settingsType>({schema});

// ERROR: TS2339: Property 'get' does not exist on type 'ElectronStore<settingsType>'.
let settingsStore: settingsType = store.get('settings');

I think I'm defining everything as expected, but I'm still getting a TS error from the store.get call. What am I missing?

Share Improve this question edited May 4, 2024 at 20:02 user101289 asked May 4, 2024 at 18:09 user101289user101289 10.4k18 gold badges91 silver badges162 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 11

Try to downgrade electron-store version to 8.2.0 npm i [email protected]. It helped in my case.

Aside from downgrading, adding "moduleResolution": "node16" to your tsconfig should also fix this issue. Also you will need to add a corresponding pilerOption.module to your tsconfig, just for good measures.

But then you will need to make sure you have "type": "module" in you package.json. Which may break, or fix, other package imports in your project. Plus you will probably need to modify your preload path when calling new BrowserWindow() to import an mjs file instead of a js one for your preload script like so: preload: join(__dirname, '../preload/index.mjs').

And if you make these changes, import paths will now need explicit file extensions so /code.js instead of /code. Plus keep in mind that global variables will now be shared so you may run into issues redeclaring vars. And in general your code will be piled drastically different than it was before... so keep that in mind.

Or just downgrade if that works for you.

本文标签: javascriptelectronstore returns quotProperty 39set3939get39 does not exist on typequotStack Overflow