Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.1k views
in Technique[技术] by (71.8m points)

typescript - How do you specify that a class property is an integer?

I'm experimenting with TypeScript, and in the process of creating a class with an ID field that should be an integer, I have gotten a little confused.

First off, in Visual Studio 2012 with the TypeScript plugin, I see int in the intelliSense list of types. But I get a compile error that says:

the name 'int' does not exist in the current scope.

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

So, I'm left with two questions:

  1. How should I indicate to users of my class that a particular field is not just a number but an integer (and never a floating point or decimal number)?

  2. Why do I see int in the intellisense list if it's not a valid type?

Update: All the answers I've gotten so far are about how JavaScript doesn't have an int type, it would be hard to enforce an int type at runtime... I know all that. I am asking if there is a TypeScript way to provide an annotation to users of my class that this field should be an integer. Perhaps a comment of some particular format?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
  1. I think there is not a direct way to specify whether a number is integer or floating point. In the TypeScript specification section 3.2.1 we can see:

    "...The Number primitive type corresponds to the similarly named JavaScript primitive type and represents double-precision 64-bit format IEEE 754 floating point values..."

  2. I think int is a bug in Visual Studio intelliSense. The correct is number.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...