【问题标题】:TypeScript - One-line conditional (":" expected, expression expected)TypeScript - 单行条件(“:”预期,表达式预期)
【发布时间】:2020-01-28 03:09:26
【问题描述】:

将 Express JS 服务器转换为 TS,以前正常运行的 JS 条件现在会抛出奇怪的错误。

示例:if (req!.body?.userObj) req.session!.userObj = req.body!.userObj

使用ts-node 运行正常,没有问题。但是运行tsc 编译会抛出这些错误:

error TS1109: Expression expected.  

55     if (req!.body?.userObj) req.session!.userObj = req.body!.userObj 
                     ~

middleware/payloadHandle.ts:55:27 - 
error TS1005: ':' expected.

55     if (req!.body?.userObj) req.session!.userObj = req.body!.userObj 

【问题讨论】:

  • 您使用的是哪个版本的tsc?. 运算符是在 TypeScript 3.7 中添加的。
  • @Dai 就是这样...不同的计算机,在 3.6 上工作
  • 为什么你不尝试让它变成这样:if (req.body.userObj) req.session.userObj = req.body.userObj; 没有?!。因为如果我没记错的话,? 是可选的,要使用这个可选的,你可以在你的界面中做这样的例子:interface myInterface { body?: string }

标签: javascript typescript express


【解决方案1】:

您确保 TS 编译器已更新:npm i typescript@latest

【讨论】:

    猜你喜欢
    • 2019-05-05
    • 2021-04-25
    • 1970-01-01
    • 1970-01-01
    • 2023-04-08
    • 2022-11-02
    • 2015-11-09
    • 2011-12-25
    • 1970-01-01
    相关资源
    最近更新 更多