Last active
March 16, 2019 22:27
-
-
Save TheUltDev/e7b3fd1ede08ee03cdd365e1d99a544e to your computer and use it in GitHub Desktop.
Redux Typing based on https://medium.com/@martin_hotell/improved-redux-type-safety-with-typescript-2-8-2c11a8062575
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
type FunctionType = (...args: any[]) => any; | |
type ActionCreatorsMapObject = {[actionCreator: string]: FunctionType}; | |
export type ActionsUnion<A extends ActionCreatorsMapObject> = ReturnType<A[keyof A]> | |
export interface Action<T extends string> { | |
type: T | |
}; | |
export interface ActionWithPayload<T extends string, P> extends Action<T> { | |
payload: P | |
}; | |
export function createAction<T extends string>(type: T): Action<T>; | |
export function createAction<T extends string, P>(type: T, payload: P): ActionWithPayload<T, P>; | |
export function createAction<T extends string, P>(type: T, payload?: P) { | |
return payload === undefined ? {type} : {type, payload}; | |
}; |
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
import {ContentItem, ContentFolder} from './ContentModels'; | |
import {createAction, ActionsUnion} from '../lib/actions'; | |
export const enum ContentActionTypes { | |
MoveItem = '[content] move item', | |
CopyItem = '[content] copy item', | |
}; | |
export const ContentActions = { | |
moveItem: (items: Array<ContentItem['id']>, destination: ContentFolder['id']) => | |
createAction(ContentActionTypes.MoveItem, {items, destination}), | |
copyItem: (items: Array<ContentItem['id']>, destination: ContentFolder['id']) => | |
createAction(ContentActionTypes.CopyItem, {items, destination}), | |
}; | |
export type ContentActions = ActionsUnion<typeof ContentActions>; |
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
export interface ContentItem { | |
readonly id: string; | |
readonly name: string; | |
readonly size: number; | |
readonly revision: number; | |
readonly parent?: ContentItem; | |
} | |
export interface ContentFile extends ContentItem { | |
readonly filetype: string; | |
} | |
export interface ContentFolder extends ContentItem { | |
readonly files: ContentFile[]; | |
readonly folders: ContentFolder[]; | |
} | |
export type ContentState = { | |
readonly items: {[id: string]: ContentItem}, | |
readonly searchItems: ContentItem[], | |
readonly recentItems: ContentItem[] | |
}; |
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
import {ContentState} from './ContentModels'; | |
import {ContentActionTypes, ContentActions} from './ContentActions'; | |
export const InitialState: ContentState = { | |
items: {}, | |
searchItems: [], | |
recentItems: [] | |
}; | |
export default (state = InitialState, action: ContentActions): ContentState => { | |
switch (action.type) { | |
case ContentActionTypes.MoveItem: { | |
return state; | |
} | |
case ContentActionTypes.CopyItem: { | |
return state; | |
} | |
default: return state; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment