Coder Social home page Coder Social logo

Comments (5)

eduter avatar eduter commented on July 19, 2024 1

I don't know what's the use case, but const data = new Map<Id<Creep>, number>() may also be an option.

from typed-screeps.

pyrodogg avatar pyrodogg commented on July 19, 2024

Game object id's were re-typed as a result of #92, which has been planned for some time. Since preparations are being made for a major version release, I thought it would be a good time to include this breaking change.

Unfortunately, object keys must be of type sting | number | symbol so Id<Creep> can't be used as the key type for a Record.

You can still make a record with a string key, and explicitly type assert the key values to strings.

const data: Record<string, number> = {};
data[creep.id as string] = 42; // type assert Id<Creep> to string

from typed-screeps.

pyrodogg avatar pyrodogg commented on July 19, 2024

Yes, Map seems like a much better way to handle these now as Map keys don't have to be primitives.

    const creepStat = new Map<Id<Creep>, number>();
    const creepId: Id<Creep> = "123";

    creepStat.set(creepId, 123);

    creepStat.forEach((number, id) => { 
        const creep = Game.getObjectById(key); // type of Creep | null

        if (creep) {
            // do something?
        }
    });

from typed-screeps.

JBYoshi avatar JBYoshi commented on July 19, 2024

What about places where the ID is used as a key in memory?

from typed-screeps.

pyrodogg avatar pyrodogg commented on July 19, 2024

Cast to string or number as index types can only be string | number | symbol types.

from typed-screeps.

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.