Giter Site home page Giter Site logo

Comments (7)

anisoptera avatar anisoptera commented on July 29, 2024

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.

bonzaiferroni avatar bonzaiferroni commented on July 29, 2024

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.

Dessix avatar Dessix commented on July 29, 2024

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.

pathurs avatar pathurs commented on July 29, 2024

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.

kriber82 avatar kriber82 commented on July 29, 2024

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.

Dessix avatar Dessix commented on July 29, 2024

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

from screeps-typescript-declarations.

kriber82 avatar kriber82 commented on July 29, 2024

@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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.