-
Notifications
You must be signed in to change notification settings - Fork 12.5k
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
Comments
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. |
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). |
Here's what I've done commonly (taking advantage of TypeScript's type inference on assigment to infert the type of let colors = {
red : 'red',
blue: 'blue',
// so on
} Now : colors.blue; // okay
colors.invalid; // Error |
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. |
There is this issue : #3192 |
Yeah, dupe of the mentioned string enums issue. |
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.
The text was updated successfully, but these errors were encountered: