代码之家  ›  专栏  ›  技术社区  ›  Leon Gaban

声明的类型既不是“void”也不是“any”的函数必须返回一个值。类型脚本/反应

  •  -1
  • Leon Gaban  · 技术社区  · 6 年前

    在我的组件中,我有一个名为authform的函数,它使用一个开关呈现3个组件中的1个。在param方法中,我传入一个类方法数组。除了如何正确地键入类方法本身之外,我已经把所有类型都记下来了。

    错误:

    (别名)接口ISignindata

    导入Isignindata

    声明的类型既不是“void”也不是“any”的函数必须返回值。ts(2355)

    enter image description here

    首先是接口和类型

    export interface ISignInData {
      email: string
      password: string
    }
    
    export interface IAccountData {
      email: string
      password: string
      password2: string
    }
    
    export interface IForgotData {
      email: string
    }
    
    export type TSignIn = (email: string, password: string) => (ISignInData)
    
    export type TCreateAccount = (email: string, password: string, password2: string) => (IAccountData)
    
    export type TForgot = (email: string) => (IForgotData)
    

    组成部分

    import React from 'react'
    
    import { Astronaut, Login, Create, Forgot, NomicsLink } from '../components'
    import { TSignIn, TCreateAccount, TForgot, ISignInData, IAccountData, IForgotData } from '../shared/types'
    import { AuthContainer } from '../styles'
    
    interface IState { view: string }
    
    interface IAuthView {
      type: string
      methods: [
        (email: string, password: string) => ISignInData,
        (email: string, password: string, password2: string) => IAccountData,
        (email: string) => IForgotData
      ]
      changeView: any
    }
    
    const AuthForm = ({ type, methods, changeView }: IAuthView) => {
      switch(type) {
        case 'signin':
          return <Login signIn={methods[0]} changeView={changeView} />
        case 'create':
          return <Create createAccount={methods[1]} changeView={changeView} />
        case 'forgot':
          return  <Forgot resetPassword={methods[2]} changeView={changeView} />
        default:
          return null;
      }
    }
    
    class Account extends React.Component<null, IState> {
      constructor(props: any) {
        super(props);
        this.state = { view: 'signin' }
      }
    
      render() {
        const { view } = this.state;
    
        return (
          <div id="login-container">
            <AuthContainer>
              {
                <AuthForm
                  type={view}
                  methods={[this.handleSignIn, this.handleCreateAccount, this.handleResetPassword]}
                  changeView={this.handleChangeView}
                />
              }
            </AuthContainer>
            <NomicsLink />
            <Astronaut className="astronaut" showLogo={false}/>
          </div>
        )
      }
    
      handleSignIn = (email: string, password: string): ISignInData => {
        console.log('handleSignIn')
      }
    
      handleCreateAccount = (email: string, password: string, password2: string): IAccountData => {
        console.log('handleSignIn')
      }
    
      handleResetPassword = (email: string): IForgotData => {
        console.log('handleResetPassword')
      }
    
      handleChangeView = (view: string) => {
        console.log('handleChangeView')
        this.setState({ view })
      }
    }
    
    export default Account
    
    1 回复  |  直到 6 年前
        1
  •  2
  •   lukasgeiter    6 年前

    你已经宣布 ISignInData 作为返回类型。但是这个方法没有返回任何内容。把它改成 void 如果你不打算归还任何东西:

    handleSignIn = (email: string, password: string): void => {
        console.log('handleSignIn')
    }
    
    推荐文章