简体   繁体   中英

TypeScript function to return nullable Promise

I'm trying to create a function that returns a Promise with values that can be resolved to null.

Basically something like this:

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

but it is returning an error

Generic type 'Promise' requires 1 type argument(s)

How to create a function in TypeScript that can return a Promise with nullable resolved values?

? means optionnal parameter not nullable type .

You should use this:

Promise<User|null>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM