How do you indicate that a class property is an integer?

I am experimenting with TypeScript, and in the process of creating a class with an "ID" field, which should be an integer, I got a little confused.

First, in Visual Studio 2012 with the TypeScript plugin, I see "int" in the list of intelliSense types. But I get a compilation error that says that the "name" int "does not exist in the current scope."

I reviewed the language specifications and looked only at the following primitive types: number, string, boolean, null, and undefined. No integer type.

So, I have two questions left:

  • How can I tell users of my class that a particular field is not just a "number", but an integer (and never a floating point or decimal)?

  • Why do I see "int" in the intellisense list if it is not a valid type?

Update. All the answers I have received so far regarding how JavaScript does not have an int type, it would be difficult to provide an int type at runtime ... I know all this. I ask if there is a TypeScript way to provide annotation to users of my class that this field should be an integer. Perhaps a comment of some specific format?

+75
typescript
Oct 15 '12 at 14:25
source share
7 answers
  • I think there is no direct way to indicate whether a number is an integer or a floating point. In Section 3.2.1 TypeScript we can see:

    "... The type of the Number primitive is the same as the JavaScript primitive and represents floating point values โ€‹โ€‹with a 64-bit IEEE 754 double-precision value ..."

  • I think int is a bug in Visual Studio intelliSense. Correctly - number .

+79
Oct. 15 '12 at 15:05
source share

TypeScript is a superset of JavaScript that has no concept of int. It has only the concept of a floating point number.

Philosophically, the amount of work that the compiler had to do to enforce only integers for a type of type TypeScript could be massive, and in some cases at compile time it would be impossible to guarantee that only integers would be assigned, so it's impossible to reliably add int in TypeScript.

When you first get intelliSense in Visual Studio, the toolkit cannot determine what to serve, so you get everything, including int, but as soon as you deal with something of a known type, you will get a reasonable intelliSense.

Examples

 var myInt: number; var myString: string; myInt. // toExponential, toFixed, toPrecision, toString myString. // charAt, charCodeAt, concat, indexOf, lastIndexOf, length and many more... 
+22
Oct. 15
source share

In TypeScript there is no integer or integer float but there is a number type, as in JavaScript. But if you want to tell the programmer that you expect an integer type, you can try using type aliases

 type integer = number; type float = number; // example: function setInt(id: integer) {} 

but it is still a number type, and you can get a number with a float .

Part of the description from the documentation:
"The alias does not actually create a new type - it creates a new name to refer to this type. The primitive alias is not very useful, although it can be used as a form of documentation."

+5
Sep 08 '18 at 11:06
source share

Well, as you saw, typescript doesn't float type data like javascript. Use only number that span all int and double at the same time; perhaps you should make a function that takes a number and checks if it is int or double , returning some state in case of error / success. Something like this as a method of your class:

 function SetN(x:number) { var is_int = parseInt(x) === parseFloat(x); if(is_int) this.n = x; return is_int; } //.. y = 10.5; if(SetN(y)) { //OK } else { //error not set y isn't a int } 

Note: it does not work for 10.0 , for example. If you do not want this, perhaps you should convert it to a string and try to find . .

+2
Oct. 15
source share

In TypeScript, you can approximate what is sometimes called an opaque type using a marker.

 // Helper for generating Opaque types. type Opaque<T, K> = T & { __opaque__: K }; // 2 opaque types created with the helper type Int = Opaque<number, 'Int'>; type ID = Opaque<number, 'ID'>; // using our types to differentiate our properties even at runtime // they are still just numbers class Foo { someId: ID; someInt: Int; } let foo = new Foo(); // compiler won't let you do this due to or markers foo.someId = 2; foo.someInt = 1; // when assigning, you have to cast to the specific type // NOTE: This is not completely type safe as you can trick the compiler // with something like foo.someId = 1.45 as ID and it won't complain. foo.someId = 2 as ID; foo.someInt = 1 as Int; // you can still consume as numbers let sum: number = foo.someId + foo.someInt; 

This will allow you to more clearly indicate in your code which types your properties expect, and the compiler will not allow you to assign a primitive value without a cast. This does not lead to additional .js output, and you can still use and use values โ€‹โ€‹as the types on which they are based. In this example, I use numbers, but you can also use strings and other types.

You can still trick the compiler into accepting something that is not Int or Id in this example, but it should appear if you are trying to set 1.45 to Int or something like that. You also have the option of creating helper functions that you use to create your values โ€‹โ€‹to provide validation at runtime.

There are many ways to create โ€œtaggedโ€ types. Here is a good article: https://michalzalecki.com/nominal-typing-in-typescript/

+1
May 25 '18 at 3:49
source share

int was reserved for future use in earlier versions of javascript (ECMAScript if you want). But this is the correct word now (where "now" is equal to "in the latest specification").

For example, in 262, it was still reserved, http://www.ecma-international.org/publications/files/ECMA-ST/Ecma-262.pdf

It would be nice to add an implemented int data type to typcript but with all the available type checking and type casting rules.

0
Oct. 15 '12 at 15:30
source share

Here is an implementation of a number interface that does not do boxing. I think that one could use this project to create an Integer type

0
Jul 02 '15 at 7:43
source share



All Articles