TS2339 属性 my属性 在类型 SetStateAction<User> 上不存在
TS2339 Property myProperty does not exist on type SetStateAction<User>
我正在尝试在 React 中使用 TypeScript 并收到一个我不明白的错误:
<html>TS2339: Property 'subEnd' does not exist on type 'SetStateAction<User>'.<br/>Property 'subEnd' does not exist on type '(prevState: User) => User'.
我试图在一个方法中使用 this
来处理我使用 useState
设置的对象,但出现错误:
function fetchUserInfo(
data: InterfaceJsonUserInfo,
setUserObject: React.Dispatch<React.SetStateAction<User>>
) {
setUserObject({
id: data.id,
subEnd: new Date(data.subEnd),
subExpired() {
const subEndDate = this.subEnd;
const nowDate = new Date();
return (subEndDate > nowDate);
},
}
这是我的界面:
export interface User {
id: string, // Uuid
subEnd: Date,
subExpired: () => boolean,
}
我不明白为什么我会收到错误消息,因为 subEnd
在用户界面上。
当我从 <User>
更改为 React.Dispatch<React.SetStateAction<any>>
时,代码确实有效,但我想指定类型而不是使用 any
。那我做错了什么?
调用subExpired
时需要指定this
为User
:
export interface User {
id: string, // Uuid
subEnd: Date,
subExpired: (this: User) => boolean,
}
我正在尝试在 React 中使用 TypeScript 并收到一个我不明白的错误:
<html>TS2339: Property 'subEnd' does not exist on type 'SetStateAction<User>'.<br/>Property 'subEnd' does not exist on type '(prevState: User) => User'.
我试图在一个方法中使用 this
来处理我使用 useState
设置的对象,但出现错误:
function fetchUserInfo(
data: InterfaceJsonUserInfo,
setUserObject: React.Dispatch<React.SetStateAction<User>>
) {
setUserObject({
id: data.id,
subEnd: new Date(data.subEnd),
subExpired() {
const subEndDate = this.subEnd;
const nowDate = new Date();
return (subEndDate > nowDate);
},
}
这是我的界面:
export interface User {
id: string, // Uuid
subEnd: Date,
subExpired: () => boolean,
}
我不明白为什么我会收到错误消息,因为 subEnd
在用户界面上。
当我从 <User>
更改为 React.Dispatch<React.SetStateAction<any>>
时,代码确实有效,但我想指定类型而不是使用 any
。那我做错了什么?
调用subExpired
时需要指定this
为User
:
export interface User {
id: string, // Uuid
subEnd: Date,
subExpired: (this: User) => boolean,
}