Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/typescript/8.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Node.js json响应的节点类型脚本genrics_Node.js_Typescript - Fatal编程技术网

Node.js json响应的节点类型脚本genrics

Node.js json响应的节点类型脚本genrics,node.js,typescript,Node.js,Typescript,抱歉,这是我第一次使用node和ts 所以我有点困惑 export const successResponseWithData = <T extends unknown>(res, data) => { return res.status(200).json(data) as T; }; export const successResponseWithData=(res,data)=>{ 将res.status(200).json(data)返回为T; }; 用法 su

抱歉,这是我第一次使用node和ts 所以我有点困惑

export const successResponseWithData = <T extends unknown>(res, data) => {
  return res.status(200).json(data) as T;
};
export const successResponseWithData=(res,data)=>{
将res.status(200).json(data)返回为T;
};
用法

successResponseWithData(res,token);

这是正确的方法吗?

在express中使用typescript,它提供了许多帮助程序方法,如下面的RequestHandler 根据我的理解,如果您试图确保
successResponseWithData
应始终为特定类型,并且您返回的响应为相同类型,则可以执行以下操作:

export const interface AuthToken {
    item1: <type1>
 };
import {RequestHandler} from 'express';

export const apiEndPoint: RequestHandler = (req, res) => {
 const resp: AuthToken = successResponseWithData();
  return res.status(200).json(resp);
};

successResponseWithData: AuthToken = _ => {
  // calculate token here
  return token;
};
export const interface AuthToken{
项目1:
};
从“express”导入{RequestHandler};
export const apident:RequestHandler=(请求,res)=>{
const resp:AuthToken=successResponseWithData();
返回res.status(200).json(resp);
};
successResponseWithData:AuthToken=\u=>{
//在这里计算令牌
返回令牌;
};
export const interface AuthToken {
    item1: <type1>
 };
import {RequestHandler} from 'express';

export const apiEndPoint: RequestHandler = (req, res) => {
 const resp: AuthToken = successResponseWithData();
  return res.status(200).json(resp);
};

successResponseWithData: AuthToken = _ => {
  // calculate token here
  return token;
};