我试图将接口向下传递以响应useState
,但是当这样做时,我会得到这个错误。
“类型的
参数不能分配给'EditProfileInterface not (() => EditProfileInterface)‘类型的参数
我做错什么了?我正试着告诉打字本检查bio
是否是字符串。我知道我可以做一些类似useState<String>('')
的事情,但是我想使用一个接口。
interface EditProfileInterface {
bio: string;
gravatar: string;
}
function EditProfile(props: any) {
const [bio, setBio] = useState<EditProfileInterface>('');
.....
}
发布于 2020-06-13 11:10:25
你应该这样做:
const [bio, setBio] = useState<EditProfileInterface['bio']>('');
这将缩小您需要的特定属性(bio
)在EditProfileInterface
接口中的范围。
发布于 2020-06-13 11:09:34
您传递一个字符串(''
)作为默认值,但使用EditProfileInterface
作为类型。EditProfileInterface
是一个定义两个字符串属性的接口。
https://stackoverflow.com/questions/62364434
复制相似问题