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

Suggestion: Union types and string constants #1285

Closed
omidkrad opened this issue Nov 26, 2014 · 2 comments
Closed

Suggestion: Union types and string constants #1285

omidkrad opened this issue Nov 26, 2014 · 2 comments
Labels
Duplicate An existing issue was already created Suggestion An idea for TypeScript

Comments

@omidkrad
Copy link

It is great to know that the next TypeScript will come with Union Types. This will allow to tighten up those previously any parameter definitions that accept more than one type. One thing that I wanted to bring up which is very common in JavaScript is parameters or objects that take documented string constants. For example assume we have the following function that takes a width and a color:

changeStyle(width, color);

My suggestion is let us include string constants in Union Types, so that we can have:

type Width = number /* pixels */ | "small" | "medium" | "large";
type Color = number | string | "white" | "black" | "red" | "green" | "blue"; 
changeStyle(width: Width, color: Color);

Then we can have tools suggest those especial strings in tooltips or intellisense, and catch more errors. For example:

shape.changeStyle(120, 0xFFFFFF); // OK
shape.changeStyle("small", "magenta"); // OK
shape.changeStyle("16pt", "yellow"); // error on width
shape.changeStyle(stringObj, "red"); // OK, if string variable is passed, just allow it

Thanks!

@DanielRosenwasser
Copy link
Member

I think we're warm on this idea, and a similar proposal can be found on #1003. I think we should continue discussion on there.

@DanielRosenwasser DanielRosenwasser added Suggestion An idea for TypeScript Duplicate An existing issue was already created labels Nov 26, 2014
@omidkrad
Copy link
Author

That's great, I didn't know it was suggested 282 issues back! :)

@microsoft microsoft locked and limited conversation to collaborators Jun 18, 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 Suggestion An idea for TypeScript
Projects
None yet
Development

No branches or pull requests

2 participants