Skip to content

Commit

Permalink
Fix TypeScript definition and rely on compilation to create typings.
Browse files Browse the repository at this point in the history
  • Loading branch information
ryanbarr committed Apr 10, 2023
1 parent 0e9270a commit 5b2eadb
Show file tree
Hide file tree
Showing 7 changed files with 31 additions and 29 deletions.
Binary file added .DS_Store
Binary file not shown.
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
node_modules
dist
node_modules
localgeneric*.tgz
17 changes: 0 additions & 17 deletions index.d.ts

This file was deleted.

15 changes: 10 additions & 5 deletions index.js → index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
export class GenericStore {
constructor(key, defaults) {
const proto = { ...GenericStore.prototype };
export class _GenericStore<T> {
public key: string;

constructor(key: string, defaults?: T) {
const proto = { ..._GenericStore.prototype };
if (defaults) Object.assign(proto, Object.getPrototypeOf(defaults));
Object.setPrototypeOf(this, proto);
Object.assign(this, defaults);
Expand All @@ -19,7 +21,7 @@ export class GenericStore {
return data ? JSON.parse(data) : null;
};

set = (value) => {
set = (value: T | Partial<T>) => {
Object.assign(this, value);
return window.localStorage.setItem(
this.key,
Expand All @@ -30,4 +32,7 @@ export class GenericStore {
};
}

export const Store = GenericStore;
type GenericStore<T> = _GenericStore<T> & T;

export const Store: new <T>(key: string, data?: T) => GenericStore<T> =
_GenericStore as any;
8 changes: 3 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,12 @@
"name": "localgeneric",
"version": "1.1.3",
"description": "Local Storage wrapper with TypeScript generics support.",
"main": "index.js",
"typings": "index.d.ts",
"main": "dist/index.js",
"types": "dist/index.d.ts",
"scripts": {
"build": "tsc",
"test": "jest --env=jsdom"
},
"files": [
"index.ts"
],
"repository": {
"type": "git",
"url": "git+https://github.com/ryanbarr/localgeneric.git"
Expand Down
2 changes: 1 addition & 1 deletion tests/store.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Store } from "../index.js";
import { Store } from "../index";

describe("string stores", () => {
const store = new Store<string>("stringStores", "J. Doe");
Expand Down
14 changes: 14 additions & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"compilerOptions": {
"target": "es5",
"module": "commonjs",
"lib": ["es2017", "es7", "es6", "dom"],
"declaration": true,
"outDir": "dist",
"strict": true,
"esModuleInterop": true
},
"files": [
"index.ts"
],
}

0 comments on commit 5b2eadb

Please sign in to comment.