Stanley
Stanley

Reputation: 2806

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?

Upvotes: 40

Views: 24537

Answers (1)

ADreNaLiNe-DJ
ADreNaLiNe-DJ

Reputation: 4919

? means optionnal parameter not nullable type.

You should use this:

Promise<User|null>

Upvotes: 64

Related Questions