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