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

Stricter type aliases #5952

Closed
d180cf opened this issue Dec 6, 2015 · 6 comments
Closed

Stricter type aliases #5952

d180cf opened this issue Dec 6, 2015 · 6 comments
Labels
Duplicate An existing issue was already created Needs Proposal This issue needs a plan that clarifies the finer details of how it could be implemented. Suggestion An idea for TypeScript

Comments

@d180cf
Copy link

d180cf commented Dec 6, 2015

Let's say we have an alias for number:

/** bits 0..3 = x, bits 4..7 = y, bit 31 = always 1*/
type coords = number;
/** x = 0, y = 0 */
const zero: coords = 0;

The zero variable, despite being of type coords, will act everywhere as though it's a plain number: the language service will say that it's a number, the compiler will treat it as a plain number and so on. However the coords type is not just a number because it has some internal structure and because not every value of type number is a value of type coords (in other words, the set of values in coords is smaller than the set of values in number).

It would be nice if the language service displayed the proper type name in such cases and if the compiler put some constraints on operations with such custom types, e.g. const zero: coords = 0 should error because 0 is of type number which may be (the compiler doesn't know) wider than coords.

@DanielRosenwasser
Copy link
Member

#364 is definitely related. So is #202 to some extent.

@DanielRosenwasser DanielRosenwasser added Suggestion An idea for TypeScript Needs Proposal This issue needs a plan that clarifies the finer details of how it could be implemented. labels Dec 6, 2015
@DanielRosenwasser
Copy link
Member

As for the language service displaying the declared name, it's probably better not to do that so that users understand what they're actually dealing with. It would be inappropriate to give users the impression that the language is doing what this proposal asks for with respect to the type keyword.

On the other hand, for a different declaration that does something close to what Haskell's newtype does, we would probably do that.

@GoToLoop
Copy link

GoToLoop commented Dec 8, 2015

As a very poor workaround I'm using empty enums. For example:

declare const enum coord {}
declare const enum size {}

type xy = [coord, coord]
type wh = [size, size]

mouseX: coord
mouseY: coord

width:  size
height: size

ellipse(x: coord, y: coord, w: size, h: size): this

@mhegazy
Copy link
Contributor

mhegazy commented Dec 8, 2015

also related #4895

@mhegazy
Copy link
Contributor

mhegazy commented Dec 9, 2015

looks like a dupe of #2477

@mhegazy mhegazy closed this as completed Dec 9, 2015
@mhegazy mhegazy added the Duplicate An existing issue was already created label Dec 9, 2015
@d180cf
Copy link
Author

d180cf commented Dec 9, 2015

@GoToLoop, that's actually a decent workaround. Thanks!

@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.
Labels
Duplicate An existing issue was already created Needs Proposal This issue needs a plan that clarifies the finer details of how it could be implemented. Suggestion An idea for TypeScript
Projects
None yet
Development

No branches or pull requests

4 participants