发布于 4年前

typescript+react: 使用dva时如何定义接口?

问题描述

如代码所示:

@connect(({ taskOperate, loading }) => ({
    taskOperate,
    loading: loading.effects,
}))

举例,model.js中实际数据是

state:{
    testObj:{value:'15',id:'1'},
    testArray:[{value:'15',id:'1'},{value:'15',id:'1'}],
}

在页面中以内dva,ts告警

绑定元素“taskOperate”隐式具有“any”类型‘

请问在此处如何定义并使用接口?

解决方案

模仿了antd-pro的ts定义,搞定了。ts初体验,真的繁琐,写起来不爽。 附简要代码:

import React from 'react'
import router from 'umi/router'
import { connect, Effect } from 'dva';
import { Dispatch, AnyAction } from 'redux';
import { LoginStateIF } from '@/interfaces/stateIF';
import { LoginPropsIF } from '@/interfaces/propsIF';
import { Button, List, InputItem, Toast } from 'antd-mobile';
import { createForm } from 'rc-form'
import BizIcon from '@/components/BizIcon'
import { StateType } from './model';
import { ConnectState } from '@/models/connect';
import { validateErrorMessage } from '@/utils/tools';
import { Reducer } from 'redux';

import Logo from '@/assets/img/logo-white.png'
import styles from './index.less'

interface LoginProps {
    dispatch: Dispatch<AnyAction>;
    userLogin: StateType;
    submitting: boolean;
}
interface LoginState {
    type: string;
    autoLogin: boolean;
}

@connect(({ login, loading }: ConnectState) => ({
    userLogin: login,
    submitting: loading.effects['login/login'],
}))
©2020 edoou.com   京ICP备16001874号-3