From 5b2eadb9af7981cb7b45a24a00c7a973f3e683d6 Mon Sep 17 00:00:00 2001 From: Ryan Barr Date: Sun, 9 Apr 2023 21:02:12 -0600 Subject: [PATCH] Fix TypeScript definition and rely on compilation to create typings. --- .DS_Store | Bin 0 -> 6148 bytes .gitignore | 4 +++- index.d.ts | 17 ----------------- index.js => index.ts | 15 ++++++++++----- package.json | 8 +++----- tests/store.test.ts | 2 +- tsconfig.json | 14 ++++++++++++++ 7 files changed, 31 insertions(+), 29 deletions(-) create mode 100644 .DS_Store delete mode 100644 index.d.ts rename index.js => index.ts (68%) create mode 100644 tsconfig.json diff --git a/.DS_Store b/.DS_Store new file mode 100644 index 0000000000000000000000000000000000000000..28591eab897e700a9c08732cd246384c180c6547 GIT binary patch literal 6148 zcmeHKF;2ul47A}SPNLvS%KZXASe=f7nh$WvAqpY|3DKwH*)X# -declare module "localgeneric"; - -declare class _GenericStore { - public key: string; - - constructor(key: string, defaults?: T); - - public get(): T; - public set(v: T | Partial): void; -} - -type GenericStore = _GenericStore & T; - -export declare const Store: new (key: string, data?: T) => GenericStore; diff --git a/index.js b/index.ts similarity index 68% rename from index.js rename to index.ts index 70c737b..ecc8200 100644 --- a/index.js +++ b/index.ts @@ -1,6 +1,8 @@ -export class GenericStore { - constructor(key, defaults) { - const proto = { ...GenericStore.prototype }; +export class _GenericStore { + 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); @@ -19,7 +21,7 @@ export class GenericStore { return data ? JSON.parse(data) : null; }; - set = (value) => { + set = (value: T | Partial) => { Object.assign(this, value); return window.localStorage.setItem( this.key, @@ -30,4 +32,7 @@ export class GenericStore { }; } -export const Store = GenericStore; +type GenericStore = _GenericStore & T; + +export const Store: new (key: string, data?: T) => GenericStore = + _GenericStore as any; diff --git a/package.json b/package.json index 877336c..12e5a6a 100644 --- a/package.json +++ b/package.json @@ -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" diff --git a/tests/store.test.ts b/tests/store.test.ts index b87d14a..7c1ea2d 100644 --- a/tests/store.test.ts +++ b/tests/store.test.ts @@ -1,4 +1,4 @@ -import { Store } from "../index.js"; +import { Store } from "../index"; describe("string stores", () => { const store = new Store("stringStores", "J. Doe"); diff --git a/tsconfig.json b/tsconfig.json new file mode 100644 index 0000000..1b0fc4a --- /dev/null +++ b/tsconfig.json @@ -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" + ], +} \ No newline at end of file