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

assigning literal values to constants #110

Open
bonzaiferroni opened this issue Jul 3, 2017 · 7 comments
Open

assigning literal values to constants #110

bonzaiferroni opened this issue Jul 3, 2017 · 7 comments

Comments

@bonzaiferroni
Copy link
Contributor

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:

declare const SOURCE_ENERGY_CAPACITY: number;

Now they are declared like this:

declare const SOURCE_ENERGY_CAPACITY: 3000;

This caused a lot of compiler errors, here is an example:

let score = SOURCE_ENERGY_CAPACITY;
if (WorldMap.roomTypeFromName(sourcePos.roomName) === ROOMTYPE_SOURCEKEEPER) {
    // TS2322: Type '4000' is not assignable to type '3000'
    score = SOURCE_ENERGY_KEEPER_CAPACITY;
}

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.

@anisoptera
Copy link
Collaborator

I don't have a super strong opinion on this. I think newer versions of TS can actually infer the number type there as being valid.

You can poke it into working by explicitly declaring let score: number, of course.

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.

@bonzaiferroni
Copy link
Contributor Author

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 let score: number workaround, but this seems superfluous.

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.

@Dessix
Copy link
Collaborator

Dessix commented Jul 9, 2017

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.

@pathurs
Copy link

pathurs commented Aug 16, 2017

When declaring a variable, if instead you are specific as to it's value:

declare var one: 1;

You are telling Typescript, that this variable should ONLY be set to this type/value.

This can also be declared as such:

declare var one: 1 | 2 | 3 | 4;

Which would only through errors if you try to set the variable to anything that is not 1, 2, 3, or 4.

@kriber82
Copy link

Disclaimer: I am in no way a typescript expert.
As far as i understand the documentation (https://www.typescriptlang.org/docs/handbook/advanced-types.html) typescript only supports string literals types.
This would be in line with typescripts (2.4.2) output on dist/screeps.d.ts.

@Dessix
Copy link
Collaborator

Dessix commented Aug 19, 2017

@kriber82 When I added these changes, numeric singleton literals were also accepted.

@kriber82
Copy link

@Dessix Your changes were and still are fine, it seems.
The typescript compiler is fine with number literal types.
I just could not find them in the documentation and misread an error my IDE reported for a typescript output.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants