我试图使用msw检索模拟api调用中的数据,同时使用类型记录。我该怎么做呢?我不断收到“属性‘电子邮件’在‘DefaultBodyType’类型中不存在”
处理程序
export const handlers: RestHandler[] = [
rest.post(`/${API_VERSION}/authentication/login`, (req, res, ctx) => {
const {email} = req.body;
console.log();
return res(
ctx.status(200),
ctx.json({
token: "abdb23231232jdsaWEDwdxaCDA",
expiresIn: 100000,
isEnabled: true,
isLocked: false,
})
);
})
];发布于 2022-10-06 09:56:01
您需要为处理程序本身提供一些类型,描述请求主体和响应主体应该是什么样子。
interface LoginRequestBody {
email: string;
}
interface LoginResponseBody {
token: string,
expiresIn: number;
isEnabled: boolean;
isLocked: boolean,
}
export const handlers = [
rest.post<LoginRequestBody, LoginResponseBody>(`/${API_VERSION}/authentication/login`, (req, res, ctx) => {
const {email} = req.body;
console.log();
return res(
ctx.status(200),
ctx.json({
token: "abdb23231232jdsaWEDwdxaCDA",
expiresIn: 100000,
isEnabled: true,
isLocked: false,
})
);
})
];您也不需要RestHandler[],因为它是推断的。
MSW维护者编写的这篇文章也有助于更多地了解类型。
https://stackoverflow.com/questions/73860561
复制相似问题