簡體   English   中英

如何防止 typescript 抱怨屬性可能未定義

[英]How can I prevent typescript from complaining that a property can be undefined

我正在寫一個 expressjs/typescript 后端 API。 我正在制作助手 function (用於裝飾器)來檢查身份驗證數據。 它接受IMyRequest數據並進行一些檢查並將其作為IMyAuthenticatedRequest返回。

但是由於某種原因,typescript 仍然抱怨req參數在authenticate function 的返回語句中 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'.

這是 typescript 抱怨的示例代碼(關於倒數第二行的 return 語句):

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 };
}

您可以使用類型保護 function

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");
  }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM