Twitter khuong291
Twitter khuong291

Reputation: 11682

Binding element 'dispatch' implicitly has an 'any' type. var dispatch: any

I follow this example about redux but I write with TypeScript.

https://codesandbox.io/s/github/reactjs/redux/tree/master/examples/todos-with-undo

I get trouble in AddTodo.tsx file.

import * as React from 'react'
import { connect } from 'react-redux'
import { addTodo } from '../actions'

const AddTodo = ({ dispatch }) => {
let input: any

return (
  <div>
    <form onSubmit={e => {
      e.preventDefault()
      if (!input.value.trim()) {
        return
      }
      dispatch(addTodo(input.value))
      input.value = ''
    }}>
      <input ref={node => {
        input = node
      }} />
      <button type="submit">
        Add Todo
      </button>
    </form>
  </div>
)
}
AddTodo = connect()(AddTodo)

export default AddTodo

The dispatch parameter does not allowed. The error says:

[ts] Binding element 'dispatch' implicitly has an 'any' type.

Upvotes: 1

Views: 3921

Answers (1)

bonheury
bonheury

Reputation: 372

Maybe is a types issue. Try to add types in your props like this:

import * as React from 'react'
import { connect } from 'react-redux'
import { addTodo } from '../actions'
import { Dispatch } from 'redux';

interface AddTodoProps { 
  dispatch : Dispatch<{}> 
 }


const AddTodo = (props : AddTodoProps) => {
let input: any

return (
  <div>
    <form onSubmit={e => {
      e.preventDefault()
      if (!input.value.trim()) {
        return
      }
      props.dispatch(addTodo(input.value))
      input.value = ''
    }}>
      <input ref={node => {
        input = node
      }} />
      <button type="submit">
        Add Todo
      </button>
    </form>
  </div>
)
}

export default connect()(AddTodo)

Upvotes: 3

Related Questions