Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

explain number being a type in terms of practice #2365

Closed

Conversation

S11001001
Copy link
Contributor

Previously it was said that

Ultimately, a static type system has to make the call over what code should be flagged as an error in its system, even if it's "valid" JavaScript that won't immediately throw an error.

So "JavaScript does not have a special runtime value for integers" is not a reason for int and float not to exist—that is a choice made by TypeScript, not one imposed by JavaScript.

I've tried to provide an alternate reason instead, in terms of what TypeScript users are likely to want to do.

@@ -21,7 +21,7 @@ Each has a corresponding type in TypeScript.
As you might expect, these are the same names you'd see if you used the JavaScript `typeof` operator on a value of those types:

- `string` represents string values like `"Hello, world"`
- `number` is for numbers like `42`. JavaScript does not have a special runtime value for integers, so there's no equivalent to `int` or `float` - everything is simply `number`
- `number` is for numbers like `42`. JavaScript users do not commonly distinguish between integers and fractional or floating-point numbers, so there's no equivalent to `int` or `float` - everything is simply `number`
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm sorta iffy on this explanation; I would instead argue that TypeScript has a number type because JavaScript has typeof x === "number". Under the hood, runtimes do have integers, you can do | 0 to explicitly tell the runtime that, and so on.

It is of course possible that TS could have had both int and float, but we'd never be able to narrow to either of them via typeof, so there would always need to be a superset of both categories. JS isn't like Python where the two types are distinct and checkable at runtime by something other than checking the value itself.

@jakebailey jakebailey closed this Jul 24, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants