electron-store icon indicating copy to clipboard operation
electron-store copied to clipboard

When adding a schema, all the types become unknown

Open adamk33n3r opened this issue 5 years ago • 5 comments

When just using the defaults key, the types are correctly assumed. But when you add a schema it makes everything unknown. As you can see in the following screenshot, my code errors because Property 'width' does not exist on type '{}'. when using a schema, but it works fine when not. It causes the .get function to return unknown and the template type of the store becomes [key]: unknown. (This happens even if you have defaults along with schema, I just removed it so it would fit into one screenshot)

image

Another weird thing you can see in the screenshot is that the return value of .get() is an object that has any of the objects keys (but not any that aren't part of the objects) when the param passed into .get doesn't exist.

adamk33n3r avatar Aug 05 '20 09:08 adamk33n3r

The .get() thing might be improved by https://github.com/sindresorhus/conf/pull/117.

sindresorhus avatar Aug 07 '20 15:08 sindresorhus

// @Superjo149 @SnosMe

sindresorhus avatar Aug 07 '20 16:08 sindresorhus

Any update on this? I was a little confused about the comment regarding the conf library...does that mean it needs to be fixed in this library as well or is there a dependency?

kendallroth avatar Oct 03 '20 15:10 kendallroth

I found out why this is by looking at the type definition files of the Schema type, you have to create an interface for the schema and import the Schema type into your file as the following img protrays :). image

I hope this helps!

KookiKodes avatar May 01 '21 22:05 KookiKodes

It would be nice if electron-store automatically typed the store object based on the JSON schema.

It is possible to generate a Typescript type statically based on a json schema literal, see https://github.com/wix-incubator/as-typed

MHebes avatar Apr 14 '22 17:04 MHebes