GithubHelp home page GithubHelp logo

leeguooooo / dva-type Goto Github PK

View Code? Open in Web Editor NEW

This project forked from lei4519/dva-type

0.0 1.0 0.0 15 KB

根据 dva model 的类型定义进行 state、dispatch 的 TS 类型推导。传入 model 类型定义,返回 state 和 actions 的类型定义。

TypeScript 100.00%

dva-type's Introduction

dva-type

安装

npm i dva-type —save-dev

说明

基于 TypeScript 4.1 版本的 Template Literal Types 特性,实现 dva models 的完整类型推导和提示。

传入项目的 models 类型定义,返回 stateactions的类型定义。

  • state 类型提示

image-20210607180531500

  • action type 类型提示

image-20210607180644700

  • action payload 类型推断

image-20210607181637266

使用

Example

  1. 定义单个 Model 类型(注意 ModelEffect 不是从 dva 中导入的)

    import { Effect, Model } from 'dva-type'
    
    interface ListModel extends Model {
      state: {
        list: any[]
      }
      effects: {
        // 定义effect 传入 payload 类型
        getList: Effect<number>
    
        // 不需要 payload 的 effect
        getInfo: Effect
      }
    }
  2. 定义项目中所有 Model 的集合(使用 type 而不是 interface

    // 使用 type 定义 models,将项目中的所有 model 进行收集
    type Models = {
      list: ListModel
      info: InfoModel
      // ...
    }
  3. Models 传入 ResolverModels 获取 stateactions 的类型

    import { ResolverModels } from 'dva-type'
    
    type State = ResolverModels<Models>['state']
    type Actions = ResolverModels<Models>['actions']
  4. 使用

    // hooks
    useSelector<State>()
    const dispatch = useDispatch<(action: Actions) => any>()
    
    // class
    const mapStateToProps = (state: State) => {}
    interface Props {
      dispatch: (action: Actions) => any
    }

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.