Skip to content

Commit

Permalink
Only apply mapped types to un-branded types (#3805)
Browse files Browse the repository at this point in the history
  • Loading branch information
invliD committed Jul 16, 2020
1 parent 45281b6 commit e23aa59
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 12 deletions.
4 changes: 3 additions & 1 deletion index.d.ts
Expand Up @@ -69,7 +69,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]>
}

/* reducers */
Expand Down
6 changes: 3 additions & 3 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Expand Up @@ -84,7 +84,7 @@
"rollup-plugin-replace": "^2.2.0",
"rollup-plugin-terser": "^5.1.1",
"rxjs": "^6.5.2",
"typescript": "^3.5.3",
"typescript": "^3.9.6",
"typings-tester": "^0.3.2"
},
"npmName": "redux",
Expand Down
25 changes: 18 additions & 7 deletions test/typescript/store.ts
Expand Up @@ -11,12 +11,16 @@ import {
} from 'redux'
import 'symbol-observable'

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

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

interface DerivedAction extends Action {
Expand All @@ -30,7 +34,8 @@ const reducer: Reducer<State> = (
b: {
c: 'c',
d: 'd'
}
},
c: brandedString
},
action: Action
): State => {
Expand All @@ -43,7 +48,8 @@ const reducerWithAction: Reducer<State, DerivedAction> = (
b: {
c: 'c',
d: 'd'
}
},
c: brandedString
},
action: DerivedAction
): State => {
Expand All @@ -58,17 +64,20 @@ const store: Store<State> = createStore(reducer)

const storeWithPreloadedState: Store<State> = createStore(reducer, {
a: 'a',
b: { c: 'c', d: 'd' }
b: { c: 'c', d: 'd' },
c: brandedString
})
// typings:expect-error
const storeWithBadPreloadedState: Store<State> = createStore(reducer, {
b: { c: 'c' }
b: { c: 'c' },
c: brandedString
})

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

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

0 comments on commit e23aa59

Please sign in to comment.