React & Typescript: Property 'id' does not exist on type 'number'

levyndra

Hello everyone who came to help. The code compiles and works, but TypeScript throws an error that Property 'id' does not exist on type 'number'. If you look deep into the hook, you can see that the step property is of type number in its interface.

The error occurs in this entry: step.id

import React, { useEffect, useState } from 'react'
import FirstStep from './steps/firstStep'
import {useForm, useStep} from 'react-hooks-helper'

interface IDefaultData2 {
    id: string
}

const steps : any = [
    {id: 'firstStep'},
    {id: 'secondStep'},
    {id: 'thirdStep'},
    {id: 'confirm'},
    {id: 'success'}
]

const UserForm: React.FC = () => {
    const {step, navigation} = useStep({
        steps,
        initialStep: 0
    })
    
    console.log(typeof(step))       // object
    console.log(typeof(step.id))    // string
    console.log(step.id)            // "firstStep"
    
    return (
        <>
        {
            (() => {
                switch(step.id){
                    case 'firstStep': return <FirstStep/>
                }
            })()
        }
        </>
    )
}

export default UserForm

What doesn't it like?

SOLUTION:

  1. Add
interface useStepType {
    step: any,
    navigation: any,
}
  1. Edit

From

const { step, navigation } = useStep({
        steps,
        initialStep: 0
    })

To

const { step, navigation }: useStepType = useStep({
        steps,
        initialStep: 0
    })

Special thanks to Tomas Gonzalez

Tomas Gonzalez

So the issue is that you are setting step as an object and not as a number,

to solve this create a new interface for type step:

interface stepType {
    id: string,
}
interface useStepType {
   step: stepType,
   navigation: any,
}

and then try to set the step to this type

    const {step, navigation}: useStepType = useStep({
    steps,
    initialStep: 0
})

この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。

侵害の場合は、連絡してください[email protected]

編集
0

コメントを追加

0

関連記事

分類Dev

property then does not exist on type void , A typescript error

分類Dev

Typescript: Property 'DB' does not exist on type 'Global'

分類Dev

Typescript property does not exist on union type

分類Dev

Typescript+React+Redux+reactrouter:"property does not exist on type IntrinsicAttributes & IntrinsicClassAttributes" passing props from parent to child

分類Dev

How to prevent "Property '...' does not exist on type 'Global'" with jsdom and typescript?

分類Dev

useRef Typescript error: Property 'current' does not exist on type 'HTMLElement'

分類Dev

Typescript - property scan does not exist on type Subject...?

分類Dev

Property 'background' does not exist on type '{}' - React Router modal

分類Dev

'Property does not exist on type 'never'

分類Dev

Property 'xxx' does not exist on type '{}'

分類Dev

TypeScript '...' does not exist on type 'typeof ...'

分類Dev

uisng multer with typescript: Property 'file' does not exist on type 'Request'.ts(2339)

分類Dev

TypeScript "Property does not exist on type" error when setting up a "Profile Details" page in Angular

分類Dev

Property 'values' does not exist on type 'ObjectConstructor'

分類Dev

Property 'values' does not exist on type 'ObjectConstructor'

分類Dev

TSLint: Property 'params' does not exist on type 'NavigationState'

分類Dev

Property 'subscribe' does not exist on type 'Subscription

分類Dev

Property 'entries' does not exist on type ObjectConstructor

分類Dev

Property 'innerText' does not exist on type 'Element'

分類Dev

Property 'VAR_PLURAL' does not exist on type

分類Dev

Property 'detailed' does not exist on type 'Console'

分類Dev

Property 'store' does not exist on type 'Readonly<AppInitialProps

分類Dev

Generic property 'enabled' does not exist on type 'Node'?

分類Dev

Property [id] does not exist on the Eloquent builder instance

分類Dev

typescript does not exist on type 'typeof object

分類Dev

How to define a property to avoid: Property 'X' does not exist on type 'Y'

分類Dev

Angular 5 to 6 Upgrade: Property 'map' does not exist on type Observable

分類Dev

how to fix property does not exist on type 'Object' in ionic 3?

分類Dev

Angular 6 : Property 'fromEvent' does not exist on type 'typeof Observable'

Related 関連記事

  1. 1

    property then does not exist on type void , A typescript error

  2. 2

    Typescript: Property 'DB' does not exist on type 'Global'

  3. 3

    Typescript property does not exist on union type

  4. 4

    Typescript+React+Redux+reactrouter:"property does not exist on type IntrinsicAttributes & IntrinsicClassAttributes" passing props from parent to child

  5. 5

    How to prevent "Property '...' does not exist on type 'Global'" with jsdom and typescript?

  6. 6

    useRef Typescript error: Property 'current' does not exist on type 'HTMLElement'

  7. 7

    Typescript - property scan does not exist on type Subject...?

  8. 8

    Property 'background' does not exist on type '{}' - React Router modal

  9. 9

    'Property does not exist on type 'never'

  10. 10

    Property 'xxx' does not exist on type '{}'

  11. 11

    TypeScript '...' does not exist on type 'typeof ...'

  12. 12

    uisng multer with typescript: Property 'file' does not exist on type 'Request'.ts(2339)

  13. 13

    TypeScript "Property does not exist on type" error when setting up a "Profile Details" page in Angular

  14. 14

    Property 'values' does not exist on type 'ObjectConstructor'

  15. 15

    Property 'values' does not exist on type 'ObjectConstructor'

  16. 16

    TSLint: Property 'params' does not exist on type 'NavigationState'

  17. 17

    Property 'subscribe' does not exist on type 'Subscription

  18. 18

    Property 'entries' does not exist on type ObjectConstructor

  19. 19

    Property 'innerText' does not exist on type 'Element'

  20. 20

    Property 'VAR_PLURAL' does not exist on type

  21. 21

    Property 'detailed' does not exist on type 'Console'

  22. 22

    Property 'store' does not exist on type 'Readonly<AppInitialProps

  23. 23

    Generic property 'enabled' does not exist on type 'Node'?

  24. 24

    Property [id] does not exist on the Eloquent builder instance

  25. 25

    typescript does not exist on type 'typeof object

  26. 26

    How to define a property to avoid: Property 'X' does not exist on type 'Y'

  27. 27

    Angular 5 to 6 Upgrade: Property 'map' does not exist on type Observable

  28. 28

    how to fix property does not exist on type 'Object' in ionic 3?

  29. 29

    Angular 6 : Property 'fromEvent' does not exist on type 'typeof Observable'

ホットタグ

アーカイブ