Last active
February 24, 2019 08:11
-
-
Save Reshetnyak/98420ed33936612ed9b948eeca682cfc to your computer and use it in GitHub Desktop.
Redux Actions class creator and actionCreatorFactory
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function actionCreator< | |
C extends new (...args) => { type: string, payload: any }, | |
T extends InstanceType<C>['type'], | |
P extends InstanceType<C>['payload'] | |
>(action: C): (payload: P) => { type: InstanceType<C>['type'], payload: P } | |
function actionCreator< | |
C extends new (...args) => { type: string }, | |
T extends InstanceType<C>['type'] | |
>(action: C): () => { type: InstanceType<C>['type'] } | |
function actionCreator< | |
C extends new (...args) => { type: string, payload?: any }, | |
T extends InstanceType<C>['type'], | |
P extends InstanceType<C>['payload'] = any | |
>(action: C) { | |
const type = new action().type; | |
return (payload?: P) => ({ | |
type, | |
payload | |
}); | |
}; | |
function classCreator<T extends string>(type: T): ActionNamespace<T> | |
function classCreator<T extends string, P>(type: T): ActionNamespacePayloaded<T, P> | |
function classCreator<T extends string, P>(type: T) { | |
// TODO: Add uniqueType function | |
return class { | |
static readonly type: T = type; | |
readonly type: T = type; | |
payload?: P; | |
static create = (payload?: P) => ({ | |
type, | |
payload | |
}) | |
} | |
// ActionNamespace.type = | |
} | |
const ccc = classCreator('hello'); | |
const cccType = new ccc().type; | |
let cccT: InstanceType<typeof ccc> = {type: 'hello'}; | |
class SayHelloAction extends classCreator('hello') { | |
} | |
const cc_t = SayHelloAction.type; | |
console.log('yaaaap', cc_t); | |
const c_ = new SayHelloAction; | |
const sayHelloA = SayHelloAction.create(); | |
sayHelloA.type; | |
let cc_: SayHelloAction = {type: 'hello'}; | |
class SayAction extends classCreator<'hel88lo', Array<number>>('hel88lo') { | |
} | |
const sayT = SayAction.type; | |
console.log('yaaaap', sayT); | |
const sayTClass = SayAction.create([2]); | |
sayTClass.type; | |
sayTClass.payload; | |
let someT: SayAction = {type: 'hel88lo', payload: [2]}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment