简体   繁体   English

Typescript - 类型“{}”上不存在属性“名称”

[英]Typescript - Property 'name' does not exist on type '{}'

I'm following a tutorial in React that creates a useForm hook to hook form input to state我正在关注 React 中的教程,该教程创建了一个 useForm 挂钩以将表单输入挂钩到 state

The hook looks like钩子看起来像

const useForm = (initial = {}) => {
    const [inputs, setInputs] = useState(initial)

    const handleChange = (e:any) => {
        let {value,name,type} = e.target
        if(type === 'number'){
             value = parseInt(value)
        }
        if(type === 'file'){
            value[0] = e.target.files
        }
        setInputs({
            ...inputs,
            [name]: value
        })
    }
    
    return{
        inputs,
        handleChange,
        resetForm,
        clearForm
    }
}

export default useForm

And it's used in the page like它在页面中使用

import useForm from "../lib/useForm";

const CreateProduct = () => {

    const {inputs, handleChange, clearForm, resetForm} = useForm({
        name: 'name test',
        price: 1234,
        description: 'description test'
    })

    return (
        <form>
            <label htmlFor="name">
                Name: 
                <input 
                    type="text" 
                    id="name" 
                    name="name" 
                    placeholder="name" 
                    value={inputs.name}
                    onChange={handleChange}
                />
            </label>
            <label htmlFor="name">
                Name: 
                <input 
                    type="number" 
                    id="price" 
                    name="price" 
                    placeholder="price" 
                    value={inputs.price}
                    onChange={handleChange}
                />
            </label>
        </form>
    );

};

export default CreateProduct;   

The tutorial is is js but I want it in typescript.该教程是 js,但我希望它在 typescript 中。

In the typescript here value={inputs.name} I get the following error Property 'name' does not exist on type '{}'.在 typescript 中,这里value={inputs.name}我收到以下错误Property 'name' does not exist on type '{}'.

How can I stop this error我怎样才能停止这个错误

I have tried to add an interface for inputs but this just errors, am I hiding the interface wrong here我试图为输入添加一个接口,但这只是错误,我在这里隐藏接口是否错误

import { string } from "prop-types";
import useForm from "../lib/useForm";

interface inputProps{
    name: string,   
    price: number,
    decription: string 
}

const CreateProduct = () => {

    const {(inputs):inputProps, handleChange, clearForm, resetForm} = useForm({
        name: 'name test',
        price: 1234,
        description: 'description test'
    })

    return (
        <form>
            <label htmlFor="name">
                Name: 
                <input 
                    type="text" 
                    id="name" 
                    name="name" 
                    placeholder="name" 
                    value={inputs.name}
                    onChange={handleChange}
                />
            </label>
            <label htmlFor="name">
                Name: 
                <input 
                    type="number" 
                    id="price" 
                    name="price" 
                    placeholder="price" 
                    value={inputs.price}
                    onChange={handleChange}
                />
            </label>
            <button type="button" onClick={clearForm}>Clear Form</button>
            <button type="button" onClick={resetForm}>Reset Form</button>
        </form>
    );

};

export default CreateProduct;

This is because you haven't typed your hook properly.这是因为你没有正确输入你的钩子。

I would suggest creating an input type and passing it in as a generic type when you define the hook, i'll show you an example.我建议在定义钩子时创建一个输入类型并将其作为泛型类型传递,我将向您展示一个示例。

type InputType = {
    name: string,   
    price: number,
    decription: string 
}
    const {inputs, handleChange, clearForm, resetForm} = useForm<InputType>({
        name: 'name test',
        price: 1234,
        description: 'description test'
    })
const useForm = <T>(initial: T) => {
    const [inputs, setInputs] = useState(initial)

    const handleChange = (e:any) => {
        let {value,name,type} = e.target
        if(type === 'number'){
             value = parseInt(value)
        }
        if(type === 'file'){
            value[0] = e.target.files
        }
        setInputs({
            ...inputs,
            [name]: value
        })
    }
    
    return{
        inputs,
        handleChange,
        resetForm,
        clearForm
    }
}

export default useForm

I've done this as a generic type so you will be able to change the props that go into that hook if you end up using it somewhere else in the future.我已将其作为通用类型完成,因此如果您将来最终在其他地方使用它,您将能够将 go 的道具更改为该钩子。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM