@@ -4,11 +4,22 @@ import * as assert from "assert";
44import { Context } from "almin" ;
55import { shallowEqual } from "shallow-equal-object" ;
66
7- export default class AlminReactContainer {
8- static create < T > (
7+ // Diff typing utilities
8+ // https://qiita.com/cotttpan/items/999fe07d079298c35e0c
9+ export type AlminReactContainerDiffKey < T extends string , U extends string > = ( { [ P in T ] : P } &
10+ { [ P in U ] : never } & { [ x : string ] : never } ) [ T ] ;
11+
12+ export type AlminReactContainerOmit < T , K extends keyof T > = Pick < T , AlminReactContainerDiffKey < keyof T , K > > ;
13+
14+ export type AlminReactContainerDiff < T , U > = AlminReactContainerOmit < T , keyof U & keyof T > ;
15+
16+ export type AlminReactContainerWeakDiff < T , U > = AlminReactContainerDiff < T , U > & { [ K in ( keyof U & keyof T ) ] ?: T [ K ] } ;
17+
18+ export class AlminReactContainer {
19+ static create < T , P > (
920 WrappedComponent : React . ComponentClass < T > ,
10- context : Context < any >
11- ) : React . ComponentClass < { } | void > {
21+ context : Context < P >
22+ ) : React . ComponentClass < AlminReactContainerWeakDiff < T , P > > {
1223 if ( process . env . NODE_ENV !== "production" ) {
1324 assert . ok (
1425 typeof WrappedComponent === "function" ,
@@ -17,14 +28,16 @@ export default class AlminReactContainer {
1728 assert . ok ( context instanceof Context , "`context` should be instance of Almin's Context" ) ;
1829 }
1930 const componentName = WrappedComponent . displayName || WrappedComponent . name ;
20- return class AlminContainer extends React . Component < T , { } > {
31+ // AlminContainer's props is <T - P> type
32+ // T is State of Store, P is custom props of the `WrappedComponent`
33+ return class AlminContainer extends React . Component < AlminReactContainerWeakDiff < T , P > > {
2134 static displayName = `AlminContainer(${ componentName } )` ;
2235
23- state : T ;
36+ state : P ;
2437 unSubscribe : ( ) => void | null ;
2538
26- constructor ( ) {
27- super ( ) ;
39+ constructor ( props : any ) {
40+ super ( props ) ;
2841 this . state = context . getState ( ) ;
2942 }
3043
@@ -49,8 +62,10 @@ export default class AlminReactContainer {
4962
5063 render ( ) {
5164 // Workaround TS2.3.1: https://github.com/Microsoft/TypeScript/pull/13288
52- return < WrappedComponent { ...this . state as any } /> ;
65+ return < WrappedComponent { ...this . state as any } { ... this . props } /> ;
5366 }
5467 } ;
5568 }
5669}
70+
71+ export default AlminReactContainer ;
0 commit comments