Skip to content

(store): Improved type-safety of JavaScript functions #2791

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

Open
wants to merge 1 commit into
base: v2
Choose a base branch
from

Conversation

theinfinit
Copy link

Proposition to overhaul the typescript definitions of store plugin.

Why?

Current approach is error prone and cumbersome to work with. Setters are basically untyped and getters require manual type provisioning for each call.

Note

As far as TypeScript types go, this is a breaking change.

Disclaimer

Everything written by hand. No form of "AI" was involved. Feel free to ask questions about the code.

Example

Example below is outlining the workflow and areas of improvement.

import { load } from '@tauri-apps/plugin-store';

// Each store has type-safety provided by a single source of truth.

// 1. Define types
// In the example we have: 4 keys and 3 possible types
type StateStore = {
  darkMode: boolean
  alwaysOnTop: boolean
  position: number
  'some-key': {
    value: number
  }
}

// 2. Pass type definition on load
const store = await load<StateStore>('store.json');

// 3. Setter 
// - Autocompletion of key's name,
// - Type of value automatically enforced by provided key
await store.set('alwaysOnTop', true); // (key: "alwaysOnTop", value: boolean)
await store.set('some-key', { value: 5 }); // (key: "some-key", value: { value: number; })

// 4. Getter 
// - Key name autocompletion,
// - Return type based on provided key.
const val = await store.get('some-key');
console.log(`[tauri-store] some-key:`, val); // { value: number } | undefined

await store.onKeyChange('darkMode', (value) => {
  console.log(`🚀 ~ store onKeyChange:`, value) // value: boolean | undefined
})

/* All possible types derived from the provided StateStore type:
  (number | boolean | { value: number })[]
 */
const storeValues = await store.values() 
console.log(`🚀 ~ storeValues:`, storeValues)

const storeEntries = await store.entries()
console.log(`🚀 ~ storeEntries:`, JSON.stringify(storeEntries))

@theinfinit theinfinit requested a review from a team as a code owner June 23, 2025 19:41
Copy link
Contributor

Package Changes Through dcb0ff2

There are 2 changes which include cli with minor, cli-js with minor

Planned Package Versions

The following package releases are the planned based on the context of changes in this pull request.

package current next
api-example 2.0.28 2.0.29
api-example-js 2.0.24 2.0.25
cli 2.2.1 2.3.0
cli-js 2.2.1 2.3.0

Add another change file through the GitHub UI by following this link.


Read about change files or the docs at github.com/jbolda/covector

@Legend-Master
Copy link
Contributor

Cool! I'm fan of this, but like you said, this is a breaking change, so we would probably need to wait for v3

@FabianLars FabianLars added this to the 3.0 milestone Jun 24, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants