GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — Prototype: Numeric Literal Types by weswigham · Pull Request #7480 · microsoft/TypeScript

Conversation

@weswigham

@weswigham Wesley Wigham (weswigham) commented Mar 11, 2016

Copy link
Copy Markdown
Member

This is a prototype for numeric literal types, in the same vein as string literal types.

All numeric syntaxes supported by JS should be supported (as far as I know) in type positions. Additionally, NaN and Infinity are builtin to the typespace as literal subclasses of number.

Also with this change:

  • The unary - operator gets contextually typed if its parent is contextually typed and it applies negation to any numeric literal type after it and returns the appropriate type. Ex: -(0x08) becomes -8.
  • Unary + and - is valid in type positions preceeding numeric literal types. Minus makes the following number into a negative number, as you may expect. (Plus is just allowed for symmetry and to clarify +Infinity)

There are tests testing guards against numeric literal types which I'll uncomment/add after #7235 is merged.

My open questions/thoughts:

  • I made it so NaN and Infinity have their respective types only when used in type positions. They are still of type number when used as values for compatibility reasons.
  • Comparability - I'd say that it's a given that we should allow comparisons of types akin to (1|3) < 2, but not assign them to one another incorrectly, it's safe to assume that this would need to use the comparable relationship reintroduced in Non-nullable types #7140. (Could also stand to add guards for numeric type unions on <=, <, >, >=) Even beyond that, if we know an enum member has the value 3, should it be assignable to a numeric literal type 2? Probably not. (Though it should likely still be comparable) I've started looking down the rabbit hole of comparing enum members with numeric literal types - it requires the introduction of the concept of another literal type associated with Enum Members to carry out effectively.
  • Unions of numbers and following the math - Right now, numeric types can flow through arithmetic operators - this is really cool (though it would be cooler if numbers were literal types by default). However, once they're inside a union, this flowing goes away. I could fix this really easily (just apply the arithmetic to all elements in the union, if both sides are unions of numeric types, then for each in each apply the arithmetic), but the downside is this is a really easy way to blow up the number of elements in a union.
  • Following the math is dicey in a nullable world - null acts as 0 in math, and undefined causes a NaN, so technically, all numeric literals after a math operation should be part of a union with NaN and the-result-if-one-or-both-parameters-is-zero. Which seems ridiculous and makes me want to just say that advanced literal types are only available in strict null mode.
  • Mutability and type safety - As with strings, mutability widening is probably a good idea for convenience... however, in addition to those measures, mutate-assignment patterns such as x += 2 where x is already of a literal type need to be disallowed (which is only an issue since I made arithmetic operators keep around numeric literal types), OR have their type updated to reflect potential new values (either widened like Mutability widening for literal types #6554 or redefined ala Control flow based type guards #6959).
  • Flowing numbers through generic parameters into inferred return values - This would make the type system able to act like a symbolic solver at compile time, which is neat. But this would need syntax for stating that a function can take some domain of numeric literal types (rather than just all subtypes of number or somesuch) as a parameter type.

I probably had more questions, but I want to put this out for people to see for now. This isn't something that should be considered ready to use, but it should be a great place to start looking at what's feasible.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

krtr, yortus, svieira, ConquestArrow, basarat, tinganho, and lydia-schow reacted with heart emoji
All reactions
  • ❤️ 7 reactions
@msftclas Microsoft Contribution License Agreements (msftclas) added the cla-already-signed label Mar 11, 2016
@weswigham
Wesley Wigham (weswigham) force-pushed the number-types branch from 9ec877a to 4d931db Compare March 14, 2016 18:41
This was referenced Mar 23, 2016
allow string literals (unions of string literals?) as a type of an indexer #7656
Closed
Index signature arguments are not type checked based on the specialized key type they are constrained to #7660
Closed
Suggestion: polymorphic object and tuple member type references through literal types #7730
Closed
@mhegazy

Mohamed Hegazy (mhegazy) commented Apr 5, 2016

Copy link
Copy Markdown
Contributor

thanks Wesley Wigham (@weswigham). I have added this to the design meeting agenda; we have not had one in a while, so hopefully we should get to it within a couple of weeks. One thing to note, we have previously talked about how literal types fit in with enums; and that has been the main reason we held back on pushing more into literal types. I suspect this would be brought back to discussion with this proposal.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@DanielRosenwasser Daniel Rosenwasser (DanielRosenwasser) added the Domain: Literal Types Unit types including string literal types, numeric literal types, Boolean literals, null, undefined label Apr 6, 2016
DanielRosenwasser
DanielRosenwasser reviewed Apr 6, 2016
View reviewed changes
Comment thread tests/baselines/reference/negativeNumericLiteralTypes.errors.txt
two = (((two ** two) - four) + (ten * two)) % three / two;


/*type True = 1;

@DanielRosenwasser Daniel Rosenwasser (DanielRosenwasser) Apr 6, 2016

Copy link
Copy Markdown
Member

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

Why is this commented out?

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@weswigham Wesley Wigham (weswigham) Apr 6, 2016

Copy link
Copy Markdown
Member Author

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

There are tests testing guards against numeric literal types which I'll uncomment/add after #7235 is merged.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@malibuzios malibuzios mentioned this pull request Apr 9, 2016
Number Literal Type support? #7982
Closed
@DanielRosenwasser Daniel Rosenwasser (DanielRosenwasser) mentioned this pull request Apr 15, 2016
Design Meeting Notes for 4/15/2016 #8112
Closed
Wesley Wigham (weswigham) added 13 commits April 26, 2016 02:10
@weswigham
starting to flesh out numeric literal types
407adc3
@weswigham
Preserve numeric literal formatting when definining numeric literal t…
87cc46e
…ypes
@weswigham
stub
d143390
@weswigham
handful of test cases to work with
4f4e381
@weswigham
Build in NaN and Infinity
a4f2789
@weswigham
change parsing a bit to make minuses better, broke some stuff in the …
d80fbe5
…process
@weswigham
Remove NaN/Infinity from testing libs
74fe1ef
@weswigham
fix tests
2afea63
@weswigham
first look at baseline changes from numeric types
ba552cc
@weswigham
Revert NaN and Infinity to number types when not used in type positions
7562a9d
@weswigham
fix lint
e346855
@weswigham
rework how number types are made to fix node 0.10 octal literals
659d2e0
@weswigham
Fix symbol baselines post-rebase
fcb4f9b
@weswigham
Wesley Wigham (weswigham) force-pushed the number-types branch from 15e8aaa to fcb4f9b Compare April 26, 2016 06:41
@weswigham

Wesley Wigham (weswigham) commented Jun 17, 2016

Copy link
Copy Markdown
Member Author

Anders Hejlsberg (@ahejlsberg) I'm not sure if you've seen this or not - I know you're probably already working on the same thing. At the very least I've probably got some good tests here for you.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@ahejlsberg Anders Hejlsberg (ahejlsberg) mentioned this pull request Jun 29, 2016
Number, enum, and boolean literal types #9407
Merged
@ahejlsberg

Anders Hejlsberg (ahejlsberg) commented Jul 28, 2016

Copy link
Copy Markdown
Member

Wesley Wigham (@weswigham) #9407 has now been merged, so I think we can close this one.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@weswigham

Wesley Wigham (weswigham) commented Jul 29, 2016

Copy link
Copy Markdown
Member Author

Anders Hejlsberg (@ahejlsberg) Agreed. The core feature from this PR has been implemented.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@weswigham Wesley Wigham (weswigham) closed this Jul 29, 2016
@weswigham
Wesley Wigham (weswigham) deleted the number-types branch May 13, 2017 23:38
@KiaraGrouwstra kiara (KiaraGrouwstra) mentioned this pull request May 18, 2017
numeric literal types disrespect arithmetics #15645
Open
@microsoft Microsoft (microsoft) locked and limited conversation to collaborators Jun 19, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Reviewers

No reviews

Uh oh!

There was an error while loading. Please reload this page.

Assignees

No one assigned

Labels

Domain: Literal Types Unit types including string literal types, numeric literal types, Boolean literals, null, undefined

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

Uh oh!

There was an error while loading. Please reload this page.

5 participants

@weswigham @mhegazy @ahejlsberg @DanielRosenwasser @msftclas
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.