Skip to content

Commit 7ab46d7

Browse files
committed
chore: add noUncheckedIndexedAccess
- add [`noUncheckedIndexedAccess`](https://www.typescriptlang.org/tsconfig/#noUncheckedIndexedAccess)
1 parent 30db7d1 commit 7ab46d7

7 files changed

Lines changed: 55 additions & 44 deletions

File tree

src/hooks/useSelector.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -236,7 +236,7 @@ export function createSelectorHook(
236236
}
237237
return selected
238238
},
239-
}[selector.name],
239+
}[selector.name]!,
240240
[selector],
241241
)
242242

src/utils/hoistStatics.ts

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -71,14 +71,16 @@ export type NonReactStatics<
7171
[key: string]: true
7272
} = {},
7373
> = {
74-
[key in Exclude<
75-
keyof Source,
76-
Source extends MemoExoticComponent<any>
77-
? keyof typeof MEMO_STATICS | keyof C
78-
: Source extends ForwardRefExoticComponent<any>
79-
? keyof typeof FORWARD_REF_STATICS | keyof C
80-
: keyof typeof REACT_STATICS | keyof typeof KNOWN_STATICS | keyof C
81-
>]: Source[key]
74+
[
75+
key in Exclude<
76+
keyof Source,
77+
Source extends MemoExoticComponent<any>
78+
? keyof typeof MEMO_STATICS | keyof C
79+
: Source extends ForwardRefExoticComponent<any>
80+
? keyof typeof FORWARD_REF_STATICS | keyof C
81+
: keyof typeof REACT_STATICS | keyof typeof KNOWN_STATICS | keyof C
82+
>
83+
]: Source[key]
8284
}
8385

8486
const defineProperty = Object.defineProperty
@@ -117,8 +119,7 @@ export default function hoistNonReactStatics<
117119
const targetStatics = getStatics(targetComponent)
118120
const sourceStatics = getStatics(sourceComponent)
119121

120-
for (let i = 0; i < keys.length; ++i) {
121-
const key = keys[i]
122+
for (const key of keys) {
122123
if (
123124
!KNOWN_STATICS[key as keyof typeof KNOWN_STATICS] &&
124125
!(sourceStatics && sourceStatics[key as keyof typeof sourceStatics]) &&

src/utils/shallowEqual.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,10 @@ export default function shallowEqual(objA: any, objB: any) {
2323

2424
if (keysA.length !== keysB.length) return false
2525

26-
for (let i = 0; i < keysA.length; i++) {
26+
for (const propertyKey of keysA) {
2727
if (
28-
!Object.prototype.hasOwnProperty.call(objB, keysA[i]) ||
29-
!is(objA[keysA[i]], objB[keysA[i]])
28+
!Object.prototype.hasOwnProperty.call(objB, propertyKey) ||
29+
!is(objA[propertyKey], objB[propertyKey])
3030
) {
3131
return false
3232
}

test/components/connect.spec.tsx

Lines changed: 36 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -722,9 +722,10 @@ describe('React', () => {
722722
)}
723723
</ProviderMock>,
724724
)
725-
expect(spy).toHaveBeenCalledOnce()
726-
expect(spy.mock.calls[0][0]).toMatch(
727-
/mapStateToProps\(\) in Connect\(Container\) must return a plain object/,
725+
expect(spy).toHaveBeenCalledExactlyOnceWith(
726+
expect.stringMatching(
727+
/mapStateToProps\(\) in Connect\(Container\) must return a plain object/,
728+
),
728729
)
729730
spy.mockRestore()
730731
rtl.cleanup()
@@ -739,9 +740,10 @@ describe('React', () => {
739740
)}
740741
</ProviderMock>,
741742
)
742-
expect(spy).toHaveBeenCalledOnce()
743-
expect(spy.mock.calls[0][0]).toMatch(
744-
/mapStateToProps\(\) in Connect\(Container\) must return a plain object/,
743+
expect(spy).toHaveBeenCalledExactlyOnceWith(
744+
expect.stringMatching(
745+
/mapStateToProps\(\) in Connect\(Container\) must return a plain object/,
746+
),
745747
)
746748
spy.mockRestore()
747749
rtl.cleanup()
@@ -756,9 +758,10 @@ describe('React', () => {
756758
)}
757759
</ProviderMock>,
758760
)
759-
expect(spy).toHaveBeenCalledOnce()
760-
expect(spy.mock.calls[0][0]).toMatch(
761-
/mapStateToProps\(\) in Connect\(Container\) must return a plain object/,
761+
expect(spy).toHaveBeenCalledExactlyOnceWith(
762+
expect.stringMatching(
763+
/mapStateToProps\(\) in Connect\(Container\) must return a plain object/,
764+
),
762765
)
763766
spy.mockRestore()
764767
rtl.cleanup()
@@ -773,9 +776,10 @@ describe('React', () => {
773776
)}
774777
</ProviderMock>,
775778
)
776-
expect(spy).toHaveBeenCalledOnce()
777-
expect(spy.mock.calls[0][0]).toMatch(
778-
/mapDispatchToProps\(\) in Connect\(Container\) must return a plain object/,
779+
expect(spy).toHaveBeenCalledExactlyOnceWith(
780+
expect.stringMatching(
781+
/mapDispatchToProps\(\) in Connect\(Container\) must return a plain object/,
782+
),
779783
)
780784
spy.mockRestore()
781785
rtl.cleanup()
@@ -790,9 +794,10 @@ describe('React', () => {
790794
)}
791795
</ProviderMock>,
792796
)
793-
expect(spy).toHaveBeenCalledOnce()
794-
expect(spy.mock.calls[0][0]).toMatch(
795-
/mapDispatchToProps\(\) in Connect\(Container\) must return a plain object/,
797+
expect(spy).toHaveBeenCalledExactlyOnceWith(
798+
expect.stringMatching(
799+
/mapDispatchToProps\(\) in Connect\(Container\) must return a plain object/,
800+
),
796801
)
797802
spy.mockRestore()
798803
rtl.cleanup()
@@ -807,9 +812,10 @@ describe('React', () => {
807812
)}
808813
</ProviderMock>,
809814
)
810-
expect(spy).toHaveBeenCalledOnce()
811-
expect(spy.mock.calls[0][0]).toMatch(
812-
/mapDispatchToProps\(\) in Connect\(Container\) must return a plain object/,
815+
expect(spy).toHaveBeenCalledExactlyOnceWith(
816+
expect.stringMatching(
817+
/mapDispatchToProps\(\) in Connect\(Container\) must return a plain object/,
818+
),
813819
)
814820
spy.mockRestore()
815821
rtl.cleanup()
@@ -824,9 +830,10 @@ describe('React', () => {
824830
)}
825831
</ProviderMock>,
826832
)
827-
expect(spy).toHaveBeenCalledOnce()
828-
expect(spy.mock.calls[0][0]).toMatch(
829-
/mergeProps\(\) in Connect\(Container\) must return a plain object/,
833+
expect(spy).toHaveBeenCalledExactlyOnceWith(
834+
expect.stringMatching(
835+
/mergeProps\(\) in Connect\(Container\) must return a plain object/,
836+
),
830837
)
831838
spy.mockRestore()
832839
rtl.cleanup()
@@ -841,9 +848,10 @@ describe('React', () => {
841848
)}
842849
</ProviderMock>,
843850
)
844-
expect(spy).toHaveBeenCalledOnce()
845-
expect(spy.mock.calls[0][0]).toMatch(
846-
/mergeProps\(\) in Connect\(Container\) must return a plain object/,
851+
expect(spy).toHaveBeenCalledExactlyOnceWith(
852+
expect.stringMatching(
853+
/mergeProps\(\) in Connect\(Container\) must return a plain object/,
854+
),
847855
)
848856
spy.mockRestore()
849857
rtl.cleanup()
@@ -858,9 +866,10 @@ describe('React', () => {
858866
)}
859867
</ProviderMock>,
860868
)
861-
expect(spy).toHaveBeenCalledOnce()
862-
expect(spy.mock.calls[0][0]).toMatch(
863-
/mergeProps\(\) in Connect\(Container\) must return a plain object/,
869+
expect(spy).toHaveBeenCalledExactlyOnceWith(
870+
expect.stringMatching(
871+
/mergeProps\(\) in Connect\(Container\) must return a plain object/,
872+
),
864873
)
865874
spy.mockRestore()
866875
})

test/components/hooks.spec.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ describe('React', () => {
9292
mapStateSpy2()
9393

9494
return {
95-
mappedProp: ownProps.list.map((id) => state.byId[id]),
95+
mappedProp: ownProps.list.map((id) => state.byId[id] ?? ''),
9696
}
9797
},
9898
)

test/typetests/react-redux-types.test-d.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -210,7 +210,7 @@ connect(mapStateToProps2, mapDispatchToProps6)(TodoApp)
210210
// Inject todos of a specific user depending on props
211211

212212
function mapStateToProps3(state: TodoState, ownProps: TodoProps): TodoState {
213-
return { todos: state.todos[ownProps.userId] }
213+
return { todos: state.todos[ownProps.userId] ?? [] }
214214
}
215215

216216
connect(mapStateToProps3)(TodoApp)
@@ -223,7 +223,7 @@ function mergeProps(
223223
ownProps: TodoProps,
224224
): { addTodo: (userId: string) => void } & TodoState {
225225
return objectAssign({}, ownProps, {
226-
todos: stateProps.todos[ownProps.userId],
226+
todos: stateProps.todos[ownProps.userId] ?? [],
227227
addTodo: (text: string) => dispatchProps.addTodo(ownProps.userId, text),
228228
})
229229
}

tsconfig.base.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
"noFallthroughCasesInSwitch": true,
1919
"noImplicitOverride": true,
2020
"noImplicitReturns": true,
21+
"noUncheckedIndexedAccess": true,
2122
"outDir": "./dist",
2223
"paths": {
2324
"@internal/*": ["./src/*"],

0 commit comments

Comments
 (0)