【问题标题】:Argument type 'string | null' not assignable to parameter type 'string | number | boolean'参数类型 'string | null' 不能分配给参数类型'string |号码 |布尔值
【发布时间】:2021-11-11 05:54:16
【问题描述】:

'string | 类型的参数null' 不可分配给类型为“string |”的参数号码 |布尔值'。 类型 'null' 不能分配给类型 'string |号码 |布尔'.ts(2345) (属性)User.token:字符串 |空

【问题讨论】:

  • 你应该明确地问这个问题(例如:“为什么编译器会抱怨这个”或“我怎样才能绕过编译器抛出错误......”)而不仅仅是粘贴错误消息。只有这样,社区才能提供有用和全面的解释

标签: angular typescript


【解决方案1】:

你可以通过! bang 运算符明确告诉 ts 这个字符串不为空,如果你真的确定它会在那里

.set('auth', user.token!)

【讨论】:

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