我有一个c#.net 8后端(后来被称为BE)来为我的react typescripfrontend(FE)提供数据.
我使用RTKQuery来获取数据,但当我假设从BE获得的Date对象实际上是一个Date对象时,我遇到了麻烦.
BE中的类:
public class Action
{
public int Id { get; set; }
public string Name { get; set; }
public DateTime? StartDate { get; set; }
}
输入FE:
export interface Action {
id: number;
name: string;
startDate: Date;
}
RTK查询端点:
actionById: builder.query<Action, number>({
query: (id: number) => ({
url: `${BASE_URL}/v1/Action/${id}`
method: 'GET',
}),
}),
BE的响应是一个JSON字符串,但我认为转换为Date对象是自动进行的.不是的. 一切都很好,直到我试图将它与另一个失败的日期进行比较.
所以问题是,如何以最好的方式解决这个问题?
我可以将FE中的类型更改为startDate: string;
以避免混淆,但有没有更好的方法?