简体   繁体   English

TSLint:表达式的类型为 `void`。 把它作为声明放在自己的行上

[英]TSLint: Expression has type `void`. Put it on its own line as a statement

When I run firebase deploy the CLI app runs the TS compiler and TSLint throws the error shown on the commented line:当我运行firebase deploy ,CLI 应用程序运行 TS 编译器,TSLint 抛出注释行中显示的错误:

express.get("/shopify/callback", async (req: Request, res: Response): Promise<Response|void> => {
 const {shop, code, state} = req.query;

 // parse the cookie string into an array. if state cookie is "", err
 const stateCookie = cookie.parse(req.headers.cookie as string || "").state;
 if (!stateCookie) return res.status(400).send("No state cookie");

 if (state !== stateCookie) return res.status(403).send('Cookie failed verification');

 const {hmac, ...params} = req.query;
 const queryParams = queryString.stringify(params);
 const hash = generateEncryptedHash(queryParams); // ERROR: /home/owner/PhpstormProjects/shopify/projectName/functions/src/index.ts:157:15 - Expression has type `void`. Put it on its own line as a statement.
 if (hash !== hmac) return res.status(400).send("HMAC validation failed");

I don't understand what change it wants, does anyone know how to handle this error?我不明白它想要什么改变,有谁知道如何处理这个错误? Here is the helper function being run on that line:这是在该行上运行的辅助函数:

const generateEncryptedHash = (params: unknown) => {
 if (typeof  SHOPIFY_API_SECRET === "string") {
  crypto.createHmac("sha256", SHOPIFY_API_SECRET).update(params as DataView).digest('hex');
 } else {
  throw Error("during generateEncryptedHash() SHOPIFY_API_SECRET was not a string")
 }
};



The full terminal error output is below:完整的终端错误输出如下:

$ firebase deploy --only functions

=== Deploying to 'projectName'...

i  deploying functions
Running command: npm --prefix "$RESOURCE_DIR" run lint

> functions@ lint /home/owner/PhpstormProjects/shopify/projectName/functions
> tslint --project tsconfig.json


ERROR: /home/owner/PhpstormProjects/shopify/projectName/functions/src/index.ts:157:15 - Expression has type `void`. Put it on its own line as a statement.

npm ERR! code ELIFECYCLE
npm ERR! errno 2
npm ERR! functions@ lint: `tslint --project tsconfig.json`
npm ERR! Exit status 2
npm ERR! 
npm ERR! Failed at the functions@ lint script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.

npm ERR! A complete log of this run can be found in:
npm ERR!     /home/owner/.npm/_logs/2019-12-01T16_46_05_460Z-debug.log

Error: functions predeploy error: Command terminated with non-zero exit code2

You don't return anything from generateEncryptedHash hence the void return type.您不会从generateEncryptedHash返回任何内容,因此返回类型为 void。 To compare the string you have to return something from that function.要比较字符串,您必须从该函数返回一些内容。 Like that:像那样:

const generateEncryptedHash = (params: unknown): string => {
 if (typeof SHOPIFY_API_SECRET === "string") {
  return crypto.createHmac("sha256", SHOPIFY_API_SECRET).update(params as DataView).digest('hex');
 } else {
  throw new Error("during generateEncryptedHash() SHOPIFY_API_SECRET was not a string")
 }
};

I'm not sure what DataView is but you can update digest only with a string/ Buffer.我不确定DataView是什么,但您只能使用字符串/缓冲区更新digest So you have to use JSON.stringify if the input is an object and that's what you want a hash from, eg .update(JSON.stringify(params))所以你必须使用JSON.stringify如果输入是一个对象并且这就是你想要的散列,例如.update(JSON.stringify(params))

UPD: you should set a type for the argument params UPD:您应该为参数params设置类型

暂无
暂无

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

相关问题 Firebase Admin SDK 错误:表达式的类型为“void”。 把它作为声明放在自己的行上 - Firebase Admin SDK ERROR: Expression has type `void`. Put it on its own line as a statement Typescript foreach 循环具有“表达式的类型为 `void`。” 错误 - Typescript foreach loop has 'Expression has type `void`.' error typescript函数声明为非空类型,但没有返回表达式 - typescript function declared a non-void type but has no return expression 如何在 eslint/tslint 中配置忽略单行语句? - How to config ignore single line statement in eslint/tslint? TS2349:此表达式不可调用。 类型 'void' 没有调用签名 - TS2349: This expression is not callable. Type 'void' has no call signatures 'items' 隐含类型为 'any',因为它没有类型注释并且在其自己的初始化程序中直接或间接引用 - 'items' implicitly has type 'any' because it does not have a type annotation and is referenced directly or indirectly in its own initializer rest隐式具有any类型,因为它没有类型注释,并在其自己的初始化程序中直接或间接引用 - rest implicitly has type any because it does not have a type annotation and is referenced directly or indirectly in its own initializer “函数表达式,缺少返回类型注释,隐式具有‘任何’返回类型”添加 void 操作时 - "Function expression, which lacks return-type annotation, implicitly has an 'any' return type" when adding void operation 方法中一行的tslint错误 - tslint errors for a line in a method 如何制作具有 function 的接口,该接口接受自己的类型并与接口实现一起使用 - How to make interface which has a function that accepts its own type and works with interface implementations
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM