【问题标题】:How can I prevent typescript from complaining that a property can be undefined如何防止打字稿抱怨属性可以未定义
【发布时间】:2020-03-15 16:45:06
【问题描述】:

我正在编写一个 expressjs/typescript 后端 API。我正在制作辅助函数(在装饰器中使用)来检查身份验证数据。它接受IMyRequest 数据并进行一些检查并将其返回为IMyAuthenticatedRequest

但由于某种原因,打字稿仍然抱怨authenticate函数的return语句中的req参数req.user可能是未定义的,即使我测试过(并抛出错误)如果它是。如何防止出现此错误?

这是错误:

Type 'IMyRequest' is not assignable to type 'IMyAuthenticatedRequest'.
Types of property 'user' are incompatible.
Type 'IUserRequestData | undefined' is not assignable to type 'IUserRequestData'.
Type 'undefined' is not assignable to type 'IUserRequestData'.

这是打字稿抱怨的示例代码(关于倒数第二行的返回语句):

import { Request, Response } from 'express';

interface IUserRequestData {
  id: number;
  firstName: string;
  lastName: string;
  company: {
    id: number;
    name: string;
  };
}

interface IAuthenticatedHeaders {
  authorization: string;
  [x: string]: any;
}

interface IMyRequest extends Request {
  user?: IUserRequestData;
  correlationId?: any;
}

interface IMyAuthenticatedRequest extends IMyRequest {
  user: IUserRequestData;
  headers: IAuthenticatedHeaders;
}

interface IAuthenticationResult {
  req: IMyAuthenticatedRequest;
  res: Response;
  user: IUserRequestData;
  authenticated: boolean;
  authorization: string;
}

export function authenticate(req: IMyRequest, res: Response): IAuthenticationResult {
  /**
   * Check the authorization information on the request object and return it.
   *
   * @param args
   */

  if (!req.user || !req.headers || !req.headers.authorization) {
    throw new Error('Authentication error');
  }

  const { authorization } = req.headers;

  return { req, res, authorization, user: req.user, authenticated: true };
}

【问题讨论】:

  • 以下是否回答了您的问题?需要更多解释吗?

标签: node.js typescript express interface


【解决方案1】:

你可以使用类型保护功能

import { Request, Response } from "express";

interface IUserRequestData {
  id: number;
  firstName: string;
  lastName: string;
  company: {
    id: number;
    name: string;
  };
}

interface IAuthenticatedHeaders {
  authorization: string;
  [x: string]: any;
}

interface IMyRequest extends Request {
  user?: IUserRequestData;
  correlationId?: any;
}

interface IMyAuthenticatedRequest extends IMyRequest {
  user: IUserRequestData;
  headers: IAuthenticatedHeaders;
}

interface IAuthenticationResult {
  req: IMyAuthenticatedRequest;
  res: Response;
  user: IUserRequestData;
  authenticated: boolean;
  authorization: string;
}

export function authenticate(
  req: IMyRequest,
  res: Response
): IAuthenticationResult {
  /**
   * Check the authorization information on the request object and return it.
   *
   * @param args
   */

  isAthenticationRequest(req);

  const { authorization } = req.headers;

  return { req, res, authorization, user: req.user, authenticated: true };
}

function isAthenticationRequest(
  req: IMyAuthenticatedRequest | IMyRequest
): asserts req is IMyAuthenticatedRequest {
  if (!req.user || !req.headers || !req.headers.authorization) {
    throw new Error("Authentication error");
  }
}

【讨论】:

    猜你喜欢
    • 2021-02-12
    • 2018-10-14
    • 2018-03-16
    • 2020-01-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多