-
Notifications
You must be signed in to change notification settings - Fork 44
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
assigning literal values to constants #110
Comments
I don't have a super strong opinion on this. I think newer versions of TS can actually infer the You can poke it into working by explicitly declaring Not all of the constants are defined this way, and it interferes a lot with actual usage when they are, so as I said, I don't feel super strongly about this. I've previously undone this when it was on the REACTIONS constant (thus making it unindexable by strings), for example. |
I thought there was probably a setting or a later version that would have this work as intended. If not, I would vote to make the types more general. I'm aware of the To have literal values as types seems a bit counterintuitive which is why I was wondering what situations this might be helpful for. It seems like the utility of a type rests partly in its generalizeability. I'm fully willing to drink the koolaid if there is a neat feature that i'm not aware of. |
Type widening allows expansion of a type outward from the default. If you initialize directly from a narrow type, that variable will be explicitly marked as that type. Implicit and inferred typings are able to be widened automatically. |
When declaring a variable, if instead you are specific as to it's value:
You are telling Typescript, that this variable should ONLY be set to this type/value. This can also be declared as such:
Which would only through errors if you try to set the variable to anything that is not 1, 2, 3, or 4. |
Disclaimer: I am in no way a typescript expert. |
@kriber82 When I added these changes, numeric singleton literals were also accepted. |
@Dessix Your changes were and still are fine, it seems. |
I recently imported screeps.d.ts and noticed that there were a few issues related to literal values assigned to game constants. Previously they were declared like this:
Now they are declared like this:
This caused a lot of compiler errors, here is an example:
I'm curious about the rationale for this specific way of declaring types and what situations that it would help. I'm also wondering if there is some setting that I can use to allow it to understand that I'm aiming for a
number
type here.The text was updated successfully, but these errors were encountered: