Comments (7)
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.
from screeps-typescript-declarations.
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.
from screeps-typescript-declarations.
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.
from screeps-typescript-declarations.
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.
from screeps-typescript-declarations.
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.
from screeps-typescript-declarations.
@kriber82 When I added these changes, numeric singleton literals were also accepted.
from screeps-typescript-declarations.
@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.
from screeps-typescript-declarations.
Related Issues (20)
- RoomPosition.inRangeTo parameter type is too narrow HOT 3
- Issues with RoomVisual HOT 2
- Room.createFlag and RoomPosition.createFlag return type is number, when it can also be a string of the Flag's name
- RoomVisual constructor
- STRUCTURE_WALL should be 'constructedWall', not 'wall'
- Interfaces instead of declared classes HOT 4
- StructureSpawn.spawnCreep; deprecation of createCreep / canCreateCreep HOT 1
- typings are deprecated HOT 1
- Question - how to make ts-jest happy at test runtime? HOT 1
- Source should be class? HOT 1
- Missing Nuke class
- StructureSpawn should be a class HOT 1
- Room.createFlag definition HOT 1
- Allow typing of default memory objects HOT 5
- Object for defining globals HOT 2
- nuker added to CONSTRUCTION_COST constant.ts
- Typescript 2 is stable HOT 3
- Creep body part typings suggestion
- creep.room can literally never be undefined but types say it can be HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from screeps-typescript-declarations.