Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix preloaded state type #4078

Merged
merged 2 commits into from
Oct 28, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
small fix to PreloadedState
  • Loading branch information
phryneas committed May 3, 2021
commit 83af794b06ca253c03235f28d1d3a33d8eba2b6f
2 changes: 1 addition & 1 deletion index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ export type PreloadedState<S> = Required<S> extends EmptyObject
? {
[K in keyof S1]?: S1[K] extends object ? PreloadedState<S1[K]> : S1[K]
}
: never
: S
: {
[K in keyof S]: S[K] extends string | number | boolean | symbol
? S[K]
Expand Down
23 changes: 23 additions & 0 deletions test/typescript/store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import {
StoreEnhancerStoreCreator,
Unsubscribe,
Observer,
PreloadedState,
CombinedState
Methuselah96 marked this conversation as resolved.
Show resolved Hide resolved
} from 'redux'
// @ts-ignore
import $$observable from '../src/utils/symbol-observable'
Expand Down Expand Up @@ -151,3 +153,24 @@ const observer: Observer<State> = {
}
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']