【问题标题】:TypeScript function to return nullable PromiseTypeScript 函数返回可为空的 Promise
【发布时间】:2017-07-10 09:58:59
【问题描述】:

我正在尝试创建一个返回 Promise 的函数,该函数的值可以解析为 null。

基本上是这样的:

static getUserById(ids: String): Promise<?User> {
  // do something
}

但它返回一个错误

通用类型“Promise”需要 1 个类型参数

如何在 TypeScript 中创建一个可以返回具有可为空解析值的 Promise 的函数?

【问题讨论】:

    标签: javascript typescript promise


    【解决方案1】:

    ? 表示 optionnal parameter 不是 nullable type

    你应该使用这个:

    Promise<User|null>
    

    【讨论】:

    • 好的...看到这个链接,其中使用了 Promiseyoutube.com/watch?v=etax3aEe2dA&t=1209s ..想知道它是如何完成的
    • @Stanley 它是视频流,而不是打字稿。
    • strictNullChecksts.config 中应该为真
    猜你喜欢
    • 2019-08-16
    • 2023-03-13
    • 1970-01-01
    • 1970-01-01
    • 2021-10-17
    • 1970-01-01
    • 2019-12-19
    • 2018-09-08
    • 2021-12-16
    相关资源
    最近更新 更多