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

Color Names #4769

Closed
BobFrankston opened this issue Sep 13, 2015 · 6 comments
Closed

Color Names #4769

BobFrankston opened this issue Sep 13, 2015 · 6 comments
Labels
Duplicate An existing issue was already created

Comments

@BobFrankston
Copy link

Typing color names is risky since there is no checking for typos. It would be nice to have named colors as in Color.Wheat or something like that. Even nice would be to type a field like backgroundColor typechecked as an HTML color though I'm not sure how to do that while keeping fully compatible with JS.

For an application that had only a limited number of valid colors I created a static class
class Kolor {
static black: string = "black";
static silver: string = "silver";
static gray: string = "gray";
..
}
it didn't solve the type checking but did reduce typos.

@iam3yal
Copy link

iam3yal commented Sep 14, 2015

In my opinion, you should use enum instead of a class for that where each member in the enum will have a numerical representation of the color instead of string, it's better for many reasons.

http://www.typescriptlang.org/Handbook#basic-types-enum

@BobFrankston
Copy link
Author

I do use enums but in this case the color names are string and I'm using them as strings. If I used an enum I'd have a more awkward syntax in converting to strings explicitly every time. (Were this C# I could get far fancier for better or worse).

@basarat
Copy link
Contributor

basarat commented Sep 14, 2015

I do use enums but in this case the color names are string and I'm using them as strings.

Here's what I've done commonly (taking advantage of TypeScript's type inference on assigment to infert the type of colors):

let colors = {
   red : 'red',
   blue: 'blue',
  // so on
}

Now :

colors.blue; // okay 
colors.invalid; // Error

@BobFrankston
Copy link
Author

Thanks. That's a reasonable solution. The only point I'm making is that this should part of the standard Typescript offering because it's so fundamental to doing HTML5.

@basarat
Copy link
Contributor

basarat commented Sep 14, 2015

There is this issue : #3192

@danquirk
Copy link
Member

Yeah, dupe of the mentioned string enums issue.

@danquirk danquirk added the Duplicate An existing issue was already created label Sep 14, 2015
@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
Projects
None yet
Development

No branches or pull requests

4 participants