diff --git a/index.d.ts b/index.d.ts index 4a4d4e0ad5..6fe4f59830 100644 --- a/index.d.ts +++ b/index.d.ts @@ -66,7 +66,7 @@ export type PreloadedState = Required extends EmptyObject ? { [K in keyof S1]?: S1[K] extends object ? PreloadedState : S1[K] } - : never + : S : { [K in keyof S]: S[K] extends string | number | boolean | symbol ? S[K] diff --git a/test/typescript/store.ts b/test/typescript/store.ts index 2d66396887..7c5ace9c9e 100644 --- a/test/typescript/store.ts +++ b/test/typescript/store.ts @@ -8,6 +8,8 @@ import { StoreEnhancerStoreCreator, Unsubscribe, Observer, + PreloadedState, + CombinedState, } from 'redux' // @ts-ignore import $$observable from '../src/utils/symbol-observable' @@ -151,3 +153,24 @@ const observer: Observer = { } const unsubscribeFromObservable = observable.subscribe(observer).unsubscribe unsubscribeFromObservable() + +// some type tests for PreloadedState +const ANY: any = {} +const notNever: PreloadedState<{ key: unknown }>['key'] = ANY as unknown +// typings:expect-error +const isNever: PreloadedState<{ key: never }>['key'] = ANY as unknown +const is5: 5 = ANY as PreloadedState<{ key: 5 }>['key'] +// typings:expect-error +const isNot5: 5 = ANY as PreloadedState<{ key: 6 }>['key'] +const isNumber: number = ANY as PreloadedState<{ key: number }>['key'] +const isString: string = ANY as PreloadedState<{ key: string }>['key'] +const isNested: { nested: string } = ANY as PreloadedState<{ + key: { nested: string } +}>['key'] +const isNestedOptional: { nested?: string } = ANY as PreloadedState<{ + key: CombinedState<{ nested: string }> +}>['key'] +// typings:expect-error +const isNestedReallyOptional: { nested: string } = ANY as PreloadedState<{ + key: CombinedState<{ nested: string }> +}>['key']