Categories
TypeScript Answers

How to specify that a class property is an integer with TypeScript?

Spread the love

Sometimes, we want to specify that a class property is an integer with TypeScript.

In this article, we’ll look at how to specify that a class property is an integer with TypeScript.

How to specify that a class property is an integer with TypeScript?

To specify that a class property is an integer with TypeScript, we can use the number type.

For instance, we write

class C {
  public myInt: number;
}

to declare the myInt class property with type number`.

We can set myInt to any number, but we can add checks to check that the number is an integer.

Conclusion

To specify that a class property is an integer with TypeScript, we can use the number type.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *