Skip to content

Commit 116aad9

Browse files
authored
Merge pull request #1 from woocommerce/iapi-allow-typed-store-to-miss-the-store-part
iAPI: Make storePart argument optional in overloads
2 parents 9d1a2ce + e9c7998 commit 116aad9

File tree

2 files changed

+38
-2
lines changed

2 files changed

+38
-2
lines changed

packages/interactivity/src/store.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -179,14 +179,14 @@ export function store< T extends object >(
179179
// Overload for when types are passed via generics and they contain state.
180180
export function store< T extends { state: object } >(
181181
namespace: string,
182-
storePart: ConvertPromisesToGenerators< DeepPartialState< T > >,
182+
storePart?: ConvertPromisesToGenerators< DeepPartialState< T > >,
183183
options?: StoreOptions
184184
): Prettify< ConvertGeneratorsToPromises< T > >;
185185

186186
// Overload for when types are passed via generics and they don't contain state.
187187
export function store< T extends object >(
188188
namespace: string,
189-
storePart: ConvertPromisesToGenerators< T >,
189+
storePart?: ConvertPromisesToGenerators< T >,
190190
options?: StoreOptions
191191
): Prettify< ConvertGeneratorsToPromises< T > >;
192192

packages/interactivity/src/test/store.ts

+36
Original file line numberDiff line numberDiff line change
@@ -281,6 +281,42 @@ describe( 'Interactivity API', () => {
281281
myStore.state.nonExistent satisfies {};
282282
};
283283
} );
284+
285+
describe( 'a typed store can be returned without adding a new store part', () => {
286+
type State = {
287+
someValue: number;
288+
};
289+
type Actions = {
290+
incrementValue: ( n: number ) => void;
291+
};
292+
293+
const { state, actions } = store< {
294+
state: State;
295+
actions: Actions;
296+
} >( 'storeWithState', {
297+
actions: {
298+
incrementValue( n ) {
299+
state.someValue += n;
300+
},
301+
},
302+
} );
303+
304+
state.someValue satisfies number;
305+
actions.incrementValue( 1 ) satisfies void;
306+
307+
const { actions: actions2 } = store< { actions: Actions } >(
308+
'storeWithoutState',
309+
{
310+
actions: {
311+
incrementValue( n ) {
312+
state.someValue += n;
313+
},
314+
},
315+
}
316+
);
317+
318+
actions2.incrementValue( 1 ) satisfies void;
319+
} );
284320
} );
285321
} );
286322
} );

0 commit comments

Comments
 (0)