Are these two generics syntax for Promise the same?


I’m new to TypeScript and learning about Generics. I’ve tried these syntax below and they work, so I’m thinking that they are the same.

Is my assumption correct?

using generic as in new Promise<number>

const promise = new Promise<number>((resolve, reject) => {
  //do smth...

using generic as in promise : Promise<number>

const promise : Promise<number> = new Promise((resolve, reject) => {
  //do smth...


In short: in practice they probably act the same way

Long: yes there is a difference, the first example you gave passes the type number as a type to the class Promise and then the constructor returns a new instance of the type Promise, in that case the type of the variable is implicit, it’s implied trough the value you give to it.

In your second example you instead do explicitly type your variable and then assign a generic Promise to it.

So: 1. Type of variable implied trough value, 2. Type explicitly given.

Answered By – Justus Flügel

Answer Checked By – Mary Flores (AngularFixing Volunteer)

Leave a Reply

Your email address will not be published.