你如何指定一个类属性是一个整数? [英] How do you specify that a class property is an integer?

查看:27
本文介绍了你如何指定一个类属性是一个整数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在试验 TypeScript,并在创建一个具有 ID 字段的类的过程中,该字段应该是 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.

首先,在带有 TypeScript 插件的 Visual Studio 2012 中,我在 intelliSense 类型列表中看到 int.但我收到一个编译错误,内容为:

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:

名称int"在当前作用域中不存在.

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

我查看了语言规范,只看到了以下原始类型:numberstringbooleannullundefined.没有 integer 类型.

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

所以,我有两个问题:

  1. 我应该如何向我班级的用户表明特定字段不仅仅是一个数字,而是一个整数(和从不浮点点或十进制数字)?

  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)?

如果不是有效类型,为什么我会在智能感知列表中看到 int?

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

更新:到目前为止我得到的所有答案都是关于 JavaScript 如何没有 int 类型,在运行时...我知道这一切.我在问是否有一种 TypeScript 方法可以向我的类的用户提供注释,该字段应该是 integer.也许是某种特定格式的评论?

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?

推荐答案

  1. 我认为没有直接的方法来指定一个数字是整数还是浮点数.在 TypeScript 规范第 3.2.1 节中我们可以看到:

  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:

...Number 基元类型对应于类似命名的 JavaScript 基元类型,表示双精度 64 位格式 IEEE 754 浮点值..."

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

  • 我认为 int 是 Visual Studio 智能感知中的一个错误.正确的是number.

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

    这篇关于你如何指定一个类属性是一个整数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

  • 查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆