admin管理员组文章数量:1313065
I'm having issues to access a object property dynamically with a Generic Type . Here is the code:
import React, { useState } from 'react'
function useForm<FormValues>(initialValues: FormValues) {
const [formValues, setFormValues] = useState<FormValues>(initialValues)
function getValue(fieldName: string) {
return formValues[fieldName]
}
}
Here is the error:
7: return formValues[fieldName] ^ Cannot get
formValues[fieldName]
because an index signature declaring the expected key / value type is missing inFormValues
[1]. References: 4: const [formValues, setFormValues] = useState(initialValues) ^ [1]
Here is the Try Flow link: +cAZlBCHAORSoYsBQXd+AdhmAR+BYgDFoIADySoIAGooANviSEAfAApg-YDGAqlq9QC44cxSrWEAlLi4BINCMLwA2nSnGb2YjEsfdQBdOABeMVJySllva3VtXX1DZSC7LjhMuAysvkEDETgAcyQYIK06YCRlABMAORQQJHM3KF0i+xwczMd2GHwoUS95NM8q2oam4O6aLmogA
Thanks for the help.
I'm having issues to access a object property dynamically with a Generic Type . Here is the code:
import React, { useState } from 'react'
function useForm<FormValues>(initialValues: FormValues) {
const [formValues, setFormValues] = useState<FormValues>(initialValues)
function getValue(fieldName: string) {
return formValues[fieldName]
}
}
Here is the error:
7: return formValues[fieldName] ^ Cannot get
formValues[fieldName]
because an index signature declaring the expected key / value type is missing inFormValues
[1]. References: 4: const [formValues, setFormValues] = useState(initialValues) ^ [1]
Here is the Try Flow link: https://flow/try/#0JYWwDg9gTgLgBAJQKYEMDGMA0cDecCuAzkgMowoxJwC+cAZlBCHAORSoYsBQXd+AdhmAR+BYgDFoIADySoIAGooANviSEAfAApg-YDGAqlq9QC44cxSrWEAlLi4BINCMLwA2nSnGb2YjEsfdQBdOABeMVJySllva3VtXX1DZSC7LjhMuAysvkEDETgAcyQYIK06YCRlABMAORQQJHM3KF0i+xwczMd2GHwoUS95NM8q2oam4O6aLmogA
Thanks for the help.
Share Improve this question asked May 17, 2019 at 13:03 Bruno QuaresmaBruno Quaresma 10.7k7 gold badges38 silver badges51 bronze badges1 Answer
Reset to default 7Lets simplify the question:
I. useState
have the following type:
export function useState<S>(
initialState: (() => S) | S,
): [S, Dispatch<BasicStateAction<S>>] {}
II. For us is important returned type:
export function simpleUseState<S>(initialState: S): [S] {}
III. Simplified version:
declare var simpleUseState: <S>(initialState: S)=> [S];
function useForm<FormValues>(initialValues: FormValues, fieldName: string) {
const [formValues] = simpleUseState<FormValues>(initialValues)
return formValues[fieldName];
}
And we have the same error
Because a Generic type has no key/value signature, for example it can be a number
or null
- we need to add constraints on Generic:
import React, { useState } from 'react'
function useForm<FormValues: {[string]: string}>(initialValues: FormValues) {
// ^ constraint
const [formValues, setFormValues] = useState<FormValues>(initialValues)
function getValue(fieldName: string) {
return formValues[fieldName]
}
}
本文标签:
版权声明:本文标题:javascript - Flow type - Index signature declaring the expected keyvalue type is missing in GenericType - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741949720a2406637.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论