@@ -70,12 +70,17 @@ export type PersistenceOptions<T, O extends Record<string, any> | undefined> = {
7070 storageOptions ?: O ;
7171 } ) ;
7272
73+ export type SignalInput = Signal < any > | [ Store < any > , SetStoreFunction < any > ] ;
74+
7375export type SignalType < S extends Signal < any > | [ Store < any > , SetStoreFunction < any > ] > =
7476 S extends Signal < infer T > ? T : S extends [ Store < infer T > , SetStoreFunction < infer T > ] ? T : never ;
7577
76- export type PersistedState < T > =
77- | [ get : Accessor < T > , set : Setter < T > , init : Promise < string > | string | null ]
78- | [ get : Store < T > , set : SetStoreFunction < T > , init : Promise < string > | string | null ] ;
78+ export type PersistedState < S extends SignalInput > =
79+ S extends Signal < infer T >
80+ ? [ get : Accessor < T > , set : Setter < T > , init : Promise < string > | string | null ]
81+ : S extends [ Store < infer T > , SetStoreFunction < infer T > ]
82+ ? [ get : Store < T > , set : SetStoreFunction < T > , init : Promise < string > | string | null ]
83+ : never ;
7984
8085/**
8186 * Persists a signal, store or similar API
@@ -97,26 +102,26 @@ export type PersistedState<T> =
97102 * @param {PersistenceOptions<T, O> } options - The options for persistence.
98103 * @returns {PersistedState<T> } - The persisted signal or store.
99104 */
100- export function makePersisted < S extends Signal < any > | [ Store < any > , SetStoreFunction < any > ] > (
105+ export function makePersisted < S extends SignalInput > (
101106 signal : S ,
102107 options ?: PersistenceOptions < SignalType < S > , undefined > ,
103- ) : PersistedState < SignalType < S > > ;
104- export function makePersisted <
105- S extends Signal < any > | [ Store < any > , SetStoreFunction < any > ] ,
106- O extends Record < string , any > ,
107- > ( signal : S , options : PersistenceOptions < SignalType < S > , O > ) : PersistedState < SignalType < S > > ;
108+ ) : PersistedState < S > ;
109+ export function makePersisted < S extends SignalInput , O extends Record < string , any > > (
110+ signal : S ,
111+ options : PersistenceOptions < SignalType < S > , O > ,
112+ ) : PersistedState < S > ;
108113export function makePersisted <
109- S extends Signal < any > | [ Store < any > , SetStoreFunction < any > ] ,
114+ S extends SignalInput ,
110115 O extends Record < string , any > | undefined ,
111116 T = SignalType < S > ,
112117> (
113118 signal : S ,
114119 options : PersistenceOptions < T , O > = { } as PersistenceOptions < T , O > ,
115- ) : PersistedState < SignalType < S > > {
120+ ) : PersistedState < S > {
116121 const storage = options . storage || ( globalThis . localStorage as Storage | undefined ) ;
117122 const name = options . name || `storage-${ createUniqueId ( ) } ` ;
118123 if ( ! storage ) {
119- return [ signal [ 0 ] , signal [ 1 ] , null ] ;
124+ return [ signal [ 0 ] , signal [ 1 ] , null ] as PersistedState < S > ;
120125 }
121126 const storageOptions = ( options as unknown as { storageOptions : O } ) . storageOptions ;
122127 const serialize : ( data : T ) => string = options . serialize || JSON . stringify . bind ( JSON ) ;
@@ -181,7 +186,7 @@ export function makePersisted<
181186 unchanged = false ;
182187 } ,
183188 init ,
184- ] as PersistedState < SignalType < S > > ;
189+ ] as PersistedState < S > ;
185190}
186191
187192/**
0 commit comments