简体   繁体   English

无法在 typescript 上正确定义 function 返回类型

[英]can't define function return type properly on typescript

This is the function that I have implemented for decoding the token with the aid of jsonwebtoken there is problem with return type that I have defined for the function,这是我为借助 jsonwebtoken 解码令牌而实现的 function 我为 function 定义的返回类型存在问题,


import jwt, { decode } from "jsonwebtoken";

export const authentication = (
  token: string
): {
  error: { [message: string]: Error } | null;
  token: { [adminID: string]: string } | null | string;
} => {
  const decodedToken = jwt.decode(token);
  return {
    error: decodedToken
      ? null
      : {
          message: new Error("invalid error"),
        },
    token: decodedToken ? decodedToken : null,
  };
};

I'm trying to utilize the function like the code snippet below我正在尝试像下面的代码片段一样使用 function

  const { token, error } = authentication(context.headers.authorization);
  if (error) {
    throw new Error(error.message.toString());
  }

  const { adminID } = token;
  console.log("this is admin id", adminID);

In this part when I'm going to get adminID by object destructuring type script throws an error in like this在这部分中,当我要通过 object 获取 adminID 时,解构类型脚本会像这样抛出错误

 Property 'adminID' does not exist on type 'string | { [adminID: string]: string; } | null'.

15   const { adminID } = token;

I need to mention that everything is working properly and I can get the adminID from token but typescript is blaming about this.我需要提一下,一切正常,我可以从token中获取adminID ,但 typescript 对此负责。 any idea would be appreciated.任何想法将不胜感激。

{ [adminID: string]: string } is an index signature : it means an object that can be indexed with any string (and the name for the string parameter is adminID ) and return a string. { [adminID: string]: string }是一个索引签名:它表示一个 object 可以用任何字符串进行索引(并且字符串参数的名称是adminID )并返回一个字符串。 For an object with an adminID property of type string you want { adminID: string } instead (and probably error: { message: Error } | null as well).对于具有string类型的adminID属性的 object,您需要{ adminID: string }代替(并且可能error: { message: Error } | null也是如此)。

And even better, use a discriminated (or tagged) union instead of two fields one of which must be null.甚至更好的是,使用区分(或标记)联合而不是两个字段,其中一个字段必须是 null。 Approximately like this:大概是这样的:

export const authentication = (
  token: string
): { kind: "error", error: Error } | { kind: "token", adminId: string }
  const decodedToken = jwt.decode(token);
  return decodedToken
      ? { kind: "token", ...decodedToken }
      : { kind: "error", error: new Error("invalid error") };
};

Of course code consuming it will need to be changed as well:当然,使用它的代码也需要更改:

const authResult = authentication(context.headers.authorization);
if (authResult.kind === "error") {
  throw new Error(authResult.error.toString());
}

const { adminID } = authResult.token;
console.log("this is admin id", adminID);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Typescript:无法从多个条件类型中定义函数的返回类型 - Typescript: can't define return type of a function from multiple conditional typings TypeScript:你能根据函数的参数定义一个返回类型结构吗? - TypeScript: Can you define a return Type structure based on an argument of the function? 如何在Typescript接口中为函数定义void的返回类型? - How can I define a return type of void for a function in a Typescript interface? Typescript 函数定义多个返回类型 - Typescript function define multiple return type Typescript:我如何最明确地定义一个可以保存 function 名称或该函数返回类型的变量? - Typescript: how do I most explicitly define a variable that can hold a function name or that function's return type? 打字稿:定义一个不能容纳任何东西的类型 - Typescript: define a type that can't hold anything 为什么 TypeScript 不能推断出这个 function 的返回类型? - Why can't TypeScript infer the return type of this function? Typescript:从 function 的返回中定义 class 类型(并将其用作类型) - Typescript: define a class type from the return of a function (and use it as a type) 在TypeScript中,定义可返回Promise的函数的最佳方法是什么? <T> 或承诺 <T[]> ? - In TypeScript, what is the best way to define a function that can return either Promise<T> or Promise<T[]>? 根据 TypeScript 中的参数在字典中定义 function 的返回类型 - Define return type of a function in dictionary based on its parameter in TypeScript
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM