From b39dcfc7619bf7ca6d373a21896067725ee77b65 Mon Sep 17 00:00:00 2001 From: Omar Luq <84993125+omarluq@users.noreply.github.com> Date: Thu, 23 Nov 2023 17:44:18 -0600 Subject: [PATCH] type validation for createStore --- src/createStore.ts | 6 ++++++ test/createStore.test.ts | 4 ++++ 2 files changed, 10 insertions(+) diff --git a/src/createStore.ts b/src/createStore.ts index ffc1eabe..36efa769 100644 --- a/src/createStore.ts +++ b/src/createStore.ts @@ -29,6 +29,7 @@ import { Store } from './store'; import type { StoreOptions } from './storeOptions'; +import { typeMap } from './storeValuesTypeMap'; export function createStore(options: StoreOptions): Store { const { name, type, initialValue } = options; @@ -39,5 +40,10 @@ export function createStore(options: StoreOptions): Store { } const symbolName = Symbol(name); + const typeConstructor = typeMap[type.name]; + if (typeof typeConstructor !== 'function') { + throw new Error(`Invalid type: ${type?.name}`); + } + return new Store(symbolName, initialValue, type); } \ No newline at end of file diff --git a/test/createStore.test.ts b/test/createStore.test.ts index 2ba189ea..65435265 100644 --- a/test/createStore.test.ts +++ b/test/createStore.test.ts @@ -15,4 +15,8 @@ describe('createStore', () => { expect(typeof store.get()).toBe('number'); expect(store.name.toString()).toBe('Symbol(testStore)'); }); + + it('should throw an error if an invalid type is provided', () => { + expect(() => createStore({ name: 'testStore', initialValue: 0, type: Set } as any)).toThrow('Invalid type: Set'); + }); }); \ No newline at end of file