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

Only apply mapped types to un-branded types #3817

Merged
merged 2 commits into from Jul 16, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 3 additions & 1 deletion src/types/store.ts
Expand Up @@ -53,7 +53,9 @@ export type PreloadedState<S> = Required<S> extends {
}
: never
: {
[K in keyof S]: S[K] extends object ? PreloadedState<S[K]> : S[K]
[K in keyof S]: S[K] extends string | number | boolean | symbol
? S[K]
: PreloadedState<S[K]>
}

/**
Expand Down
35 changes: 25 additions & 10 deletions test/typescript/store.ts
Expand Up @@ -10,12 +10,16 @@ import {
} from '../..'
import 'symbol-observable'

type BrandedString = string & { _brand: 'type' }
const brandedString = 'a string' as BrandedString

type State = {
a: 'a'
b: {
c: 'c'
d: 'd'
}
e: BrandedString
}

/* extended state */
Expand All @@ -24,7 +28,8 @@ const noExtend: ExtendState<State, never> = {
b: {
c: 'c',
d: 'd'
}
},
e: brandedString
}
// typings:expect-error
const noExtendError: ExtendState<State, never> = {
Expand All @@ -33,7 +38,8 @@ const noExtendError: ExtendState<State, never> = {
c: 'c',
d: 'd'
},
e: 'oops'
e: brandedString,
f: 'oops'
}

const yesExtend: ExtendState<State, { yes: 'we can' }> = {
Expand All @@ -42,6 +48,7 @@ const yesExtend: ExtendState<State, { yes: 'we can' }> = {
c: 'c',
d: 'd'
},
e: brandedString,
yes: 'we can'
}
// typings:expect-error
Expand All @@ -50,7 +57,8 @@ const yesExtendError: ExtendState<State, { yes: 'we can' }> = {
b: {
c: 'c',
d: 'd'
}
},
e: brandedString
}

interface DerivedAction extends Action {
Expand All @@ -64,7 +72,8 @@ const reducer: Reducer<State> = (
b: {
c: 'c',
d: 'd'
}
},
e: brandedString
},
action: Action
): State => {
Expand All @@ -77,7 +86,8 @@ const reducerWithAction: Reducer<State, DerivedAction> = (
b: {
c: 'c',
d: 'd'
}
},
e: brandedString
},
action: DerivedAction
): State => {
Expand All @@ -95,17 +105,20 @@ const arrayReducer = (state: any[] = []) => state || []
const storeWithArrayState: Store<any[]> = createStore(arrayReducer)
const storeWithPreloadedState: Store<State> = createStore(reducer, {
a: 'a',
b: { c: 'c', d: 'd' }
b: { c: 'c', d: 'd' },
e: brandedString
})
// typings:expect-error
const storeWithBadPreloadedState: Store<State> = createStore(reducer, {
b: { c: 'c' }
b: { c: 'c' },
e: brandedString
})

const storeWithActionReducer = createStore(reducerWithAction)
const storeWithActionReducerAndPreloadedState = createStore(reducerWithAction, {
a: 'a',
b: { c: 'c', d: 'd' }
b: { c: 'c', d: 'd' },
e: brandedString
})
funcWithStore(storeWithActionReducer)
funcWithStore(storeWithActionReducerAndPreloadedState)
Expand All @@ -114,7 +127,8 @@ funcWithStore(storeWithActionReducerAndPreloadedState)
const storeWithActionReducerAndBadPreloadedState = createStore(
reducerWithAction,
{
b: { c: 'c' }
b: { c: 'c' },
e: brandedString
}
)

Expand All @@ -126,7 +140,8 @@ const storeWithPreloadedStateAndEnhancer: Store<State> = createStore(
reducer,
{
a: 'a',
b: { c: 'c', d: 'd' }
b: { c: 'c', d: 'd' },
e: brandedString
},
enhancer
)
Expand Down