├── .eslintignore ├── .gitignore ├── .prettierignore ├── .prettierrc ├── .gitattributes ├── tsconfig.json ├── jest.config.js ├── .editorconfig ├── .eslintrc ├── lint-staged.config.js ├── .travis.yml ├── src ├── index.ts └── index.test.ts ├── .all-contributorsrc ├── .github ├── ISSUE_TEMPLATE.md └── PULL_REQUEST_TEMPLATE.md ├── license ├── package.json └── readme.md /.eslintignore: -------------------------------------------------------------------------------- 1 | **/coverage/** 2 | dest -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules 2 | coverage 3 | dest -------------------------------------------------------------------------------- /.prettierignore: -------------------------------------------------------------------------------- 1 | dest 2 | coverage 3 | .github 4 | package.json 5 | -------------------------------------------------------------------------------- /.prettierrc: -------------------------------------------------------------------------------- 1 | semi: false 2 | singleQuote: true 3 | trailingComma: es5 4 | -------------------------------------------------------------------------------- /.gitattributes: -------------------------------------------------------------------------------- 1 | * text=auto 2 | *.js text eol=lf 3 | *.lock binary 4 | package-lock.json binary 5 | -------------------------------------------------------------------------------- /tsconfig.json: -------------------------------------------------------------------------------- 1 | { 2 | "extends": "@akameco/tsconfig", 3 | "compilerOptions": { 4 | "outDir": "dest" 5 | } 6 | } 7 | -------------------------------------------------------------------------------- /jest.config.js: -------------------------------------------------------------------------------- 1 | module.exports = { 2 | preset: 'ts-jest', 3 | testEnvironment: 'node', 4 | modulePathIgnorePatterns: ['dest'], 5 | } 6 | -------------------------------------------------------------------------------- /.editorconfig: -------------------------------------------------------------------------------- 1 | root = true 2 | 3 | [*] 4 | indent_style = space 5 | indent_size = 2 6 | end_of_line = lf 7 | charset = utf-8 8 | trim_trailing_whitespace = true 9 | insert_final_newline = true 10 | -------------------------------------------------------------------------------- /.eslintrc: -------------------------------------------------------------------------------- 1 | { 2 | "extends": ["precure/auto"], 3 | "rules": { 4 | "@typescript-eslint/no-explicit-any": "off", 5 | "@typescript-eslint/explicit-function-return-type": "off", 6 | "import/named": "off" 7 | } 8 | } 9 | -------------------------------------------------------------------------------- /lint-staged.config.js: -------------------------------------------------------------------------------- 1 | module.exports = { 2 | '*.+(js|jsx|ts|tsx)': ['eslint --fix', 'jest --findRelatedTests'], 3 | '*.+(js|jsx|json|yml|yaml|css|less|scss|ts|tsx|md|graphql|mdx)': [ 4 | 'prettier --write', 5 | ], 6 | } 7 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: node_js 2 | node_js: 3 | - stable 4 | 5 | cache: 6 | yarn: true 7 | directories: 8 | - ".eslintcache" 9 | - "node_modules" 10 | 11 | script: 12 | - yarn run build 13 | - yarn run lint 14 | - yarn run test:ci 15 | 16 | notifications: 17 | email: false 18 | 19 | branches: 20 | only: master 21 | -------------------------------------------------------------------------------- /src/index.ts: -------------------------------------------------------------------------------- 1 | import { AnyAction } from 'redux' 2 | 3 | export type ActionsType = { 4 | [Key in keyof ActionCreators]: ActionCreators[Key] extends ( 5 | ...args: any[] 6 | ) => AnyAction 7 | ? ReturnType 8 | : never 9 | } 10 | 11 | export type ActionType< 12 | ActionCreators extends object, 13 | Actions = ActionsType 14 | > = { [Key in keyof Actions]: Actions[Key] }[keyof Actions] 15 | -------------------------------------------------------------------------------- /.all-contributorsrc: -------------------------------------------------------------------------------- 1 | { 2 | "projectName": "redux-actions-type", 3 | "projectOwner": "akameco", 4 | "files": ["readme.md"], 5 | "commit": true, 6 | "contributors": [ 7 | { 8 | "login": "akameco", 9 | "name": "akameco", 10 | "avatar_url": "https://avatars2.githubusercontent.com/u/4002137?v=4", 11 | "profile": "http://akameco.github.io", 12 | "contributions": [ 13 | "code", 14 | "doc", 15 | "test", 16 | "infra" 17 | ] 18 | } 19 | ] 20 | } 21 | -------------------------------------------------------------------------------- /.github/ISSUE_TEMPLATE.md: -------------------------------------------------------------------------------- 1 | 8 | 9 | - version: 10 | - `node` version: 11 | - `npm` (or `yarn`) version: 12 | 13 | **Do you want to request a *feature* or report a *bug*?:** 14 | 15 | **What is the current behavior?:** 16 | 17 | **What is the expected behavior?:** 18 | 19 | **Suggested solution:** 20 | -------------------------------------------------------------------------------- /.github/PULL_REQUEST_TEMPLATE.md: -------------------------------------------------------------------------------- 1 | 5 | 6 | 7 | **What**: 8 | 9 | 10 | 11 | **Why**: 12 | 13 | 14 | 15 | **How**: 16 | 17 | 18 | **Checklist**: 19 | 20 | 21 | * [ ] Documentation 22 | * [ ] Tests 23 | * [ ] Ready to be merged 24 | * [ ] Added myself to contributors table 25 | 26 | 27 | -------------------------------------------------------------------------------- /license: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) akameco (akameco.github.io) 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: 6 | 7 | The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. 8 | 9 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 10 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "redux-actions-type", 3 | "version": "1.0.0", 4 | "description": "easy typing of redux action", 5 | "license": "MIT", 6 | "repository": "akameco/redux-actions-type", 7 | "author": "akameco (https://akameco.github.io)", 8 | "files": [ 9 | "dest" 10 | ], 11 | "main": "dest/index.js", 12 | "types": "dest/index.d.ts", 13 | "keywords": [ 14 | "redux", 15 | "type", 16 | "typing", 17 | "action", 18 | "action-creater" 19 | ], 20 | "engines": { 21 | "node": ">=8" 22 | }, 23 | "scripts": { 24 | "contributors:add": "all-contributors add", 25 | "prebuild": "del dest", 26 | "build": "tsc", 27 | "prepublish": "yarn build && del dest/**/*.test.*", 28 | "fmt": "prettier --write '**/*.{ts,js,json,md}'", 29 | "lint": "eslint src/**/*.ts", 30 | "test": "jest", 31 | "test:ci": "jest --coverage --ci --runInBand" 32 | }, 33 | "husky": { 34 | "hooks": { 35 | "pre-commit": "lint-staged" 36 | } 37 | }, 38 | "devDependencies": { 39 | "@akameco/tsconfig": "^0.4.0", 40 | "@types/jest": "^25.1.4", 41 | "@types/node": "^13.9.1", 42 | "all-contributors-cli": "^6.8.0", 43 | "del-cli": "^3.0.0", 44 | "eslint": "^6.0.1", 45 | "eslint-config-precure": "^5.3.1", 46 | "husky": "^4.2.3", 47 | "jest": "^25.1.0", 48 | "lint-staged": "^10.0.8", 49 | "prettier": "^1.18.2", 50 | "redux": "^4.0.4", 51 | "ts-jest": "^25.2.1", 52 | "typescript": "^3.5.3" 53 | }, 54 | "peerDependencies": { 55 | "redux": "*" 56 | } 57 | } 58 | -------------------------------------------------------------------------------- /src/index.test.ts: -------------------------------------------------------------------------------- 1 | import { ActionType, ActionsType } from '.' 2 | 3 | const increment = (payload: number) => 4 | ({ 5 | type: 'increment', 6 | payload, 7 | } as const) 8 | 9 | const decrement = (payload: number) => 10 | ({ 11 | type: 'decrement', 12 | payload, 13 | } as const) 14 | 15 | const typo = (payload: string) => 16 | ({ 17 | // typo... 18 | typo: 'typo', 19 | payload, 20 | } as const) 21 | 22 | const actions = { increment, decrement, typo } 23 | 24 | /** 25 | * @example 26 | * type Actions = { 27 | * inc: { 28 | * type: "increment"; 29 | * payload: number; 30 | * }; 31 | * decrement: { 32 | * type: "decrement"; 33 | * payload: number; 34 | * }; 35 | * typo: never; 36 | * } 37 | */ 38 | type Actions = ActionsType 39 | 40 | /** 41 | * @example 42 | * type Action = { 43 | * type: "increment"; 44 | * payload: number; 45 | * } | { 46 | * type: "decrement"; 47 | * payload: number; 48 | * } 49 | */ 50 | type Action = ActionType 51 | 52 | /** 53 | * @example 54 | * type IncrementAction = { 55 | * type: "increment"; 56 | * payload: number; 57 | * } 58 | */ 59 | type IncrementAction = Actions['increment'] 60 | 61 | test('type check', () => { 62 | const action1: Actions['increment'] = { type: 'increment', payload: 1 } 63 | const action2: Action = { type: 'increment', payload: 1 } 64 | const action3: IncrementAction = { type: 'increment', payload: 1 } 65 | expect(action1).toStrictEqual(action2) 66 | expect(action1).toStrictEqual(action3) 67 | }) 68 | 69 | type State = { 70 | count: number 71 | } 72 | 73 | export const reducer = (state: State = { count: 0 }, action: Action): State => { 74 | switch (action.type) { 75 | case 'increment': 76 | return { count: state.count + action.payload } 77 | case 'decrement': 78 | return { count: state.count - action.payload } 79 | // Type Error... 80 | // case 'typo': 81 | // return { count: action.payload } 82 | default: 83 | return state 84 | } 85 | } 86 | 87 | test('type check with reducer', () => { 88 | expect(reducer({ count: 1 }, increment(10))).toStrictEqual({ count: 11 }) 89 | }) 90 | -------------------------------------------------------------------------------- /readme.md: -------------------------------------------------------------------------------- 1 | # redux-actions-type 2 | 3 | [![Build Status](https://travis-ci.com/akameco/redux-actions-type.svg?branch=master)](https://travis-ci.com/akameco/redux-actions-type) 4 | [![tested with jest](https://img.shields.io/badge/tested_with-jest-99424f.svg)](https://github.com/facebook/jest) 5 | [![styled with prettier](https://img.shields.io/badge/styled_with-prettier-ff69b4.svg)](https://github.com/prettier/prettier) 6 | [![All Contributors](https://img.shields.io/badge/all_contributors-1-orange.svg?style=flat-square)](#contributors) 7 | 8 | > easy typing of redux action 9 | 10 | ## Install 11 | 12 | ``` 13 | $ npm install redux-actions-type 14 | ``` 15 | 16 | ``` 17 | $ yarn add redux-actions-type 18 | ``` 19 | 20 | ## Usage 21 | 22 | actions.ts 23 | 24 | ```ts 25 | export const increment = (payload: number) => 26 | ({ 27 | type: 'increment', 28 | payload, 29 | } as const) 30 | 31 | export const decrement = (payload: number) => 32 | ({ 33 | type: 'decrement', 34 | payload, 35 | } as const) 36 | 37 | export const typo = (payload: string) => 38 | ({ 39 | // typo... 40 | typo: 'typo', 41 | payload, 42 | } as const) 43 | ``` 44 | 45 | types.ts 46 | 47 | ```ts 48 | import { Reducer } from 'redux' 49 | import { ActionType, ActionsType } from 'redux-actions-type' 50 | import * as actions from './actions' 51 | 52 | /** 53 | * @example 54 | * type Action = { 55 | * type: "increment"; 56 | * payload: number; 57 | * } | { 58 | * type: "decrement"; 59 | * payload: number; 60 | * } 61 | */ 62 | type Action = ActionType 63 | 64 | /** 65 | * @example 66 | * type Actions = { 67 | * inc: { 68 | * type: "increment"; 69 | * payload: number; 70 | * }; 71 | * decrement: { 72 | * type: "decrement"; 73 | * payload: number; 74 | * }; 75 | * typo: never; 76 | * } 77 | */ 78 | type Actions = ActionsType 79 | 80 | /** 81 | * type IncrementAction = { 82 | * type: "increment"; 83 | * payload: number; 84 | * } 85 | */ 86 | type IncrementAction = Actions['increment'] 87 | 88 | interface State { 89 | count: number 90 | } 91 | 92 | const reducer: Reducer = ( 93 | state = { count: 0 }, 94 | action: Action 95 | ) => { 96 | switch (action.type) { 97 | case 'increment': 98 | return { count: state.count + action.payload } 99 | case 'decrement': 100 | return { count: state.count - action.payload } 101 | // Type Error... 102 | // case 'typo': 103 | // return { count: action.payload } 104 | default: 105 | return state 106 | } 107 | } 108 | ``` 109 | 110 | ## Contributors 111 | 112 | Thanks goes to these wonderful people ([emoji key](https://github.com/kentcdodds/all-contributors#emoji-key)): 113 | 114 | 115 | 116 |
akameco
akameco

💻 📖 ⚠️ 🚇
117 | 118 | 119 | 120 | This project follows the [all-contributors](https://github.com/kentcdodds/all-contributors) specification. Contributions of any kind welcome! 121 | 122 | ## License 123 | 124 | MIT © [akameco](http://akameco.github.io) 125 | --------------------------------------------------------------------------------