GlassBricks / typed-factorio

Complete and featureful Typescript defintions for the Factorio modding API
MIT License
33 stars 3 forks source link

Add runtime equivalent of PrototypeMap #22

Closed Dimava closed 2 weeks ago

Dimava commented 9 months ago

I have an LuaEntity and want to make something like

function isEntityType<T extends keyof EntityTypeMap>(type: T, entity: LuaEntity): entity is EntityTypeMap[T} {
  return entity.type === type;
}

to make sorta type autoinferring, but this requires something like PrototypeMap for factorio:runtime

Dimava commented 9 months ago

🤔 I'm not sure if this actually makes sense though

GlassBricks commented 8 months ago

The types of LuaEntity don't fit one-on-one with the "subclasses" of LuaEntity, so I'm not sure an EntityTypeMap the same way as PrototypeMap will work nicely.

It might be possible to define this manually, but this might be error prone and hard to maintain (e.g. "underground-belt": EntityWithOwner & { type: "underground-belt"). Would this be useful?

Dimava commented 8 months ago

At the very least, having a list of entity.name possible values may be very useful for autocompletion of if (entity.name === '...') and surface.create_entity({name: '...'})

So you may just make that basically Record<'...', {}> for now to use it as keyof, and later enhance as needed

The whole list of name was in docs somewhere iirc

... Nevermind, I've missed that name is not type so not applicable to some of surface.createEntity. Would be good to allow type?: 'inserter' if it would be ignored there anyways tho

Dimava commented 8 months ago

The thing I would expect to be the most useful is the types for stuff like Can only be used if this is ResourceEntity - so I sorta need that ResourceEntity as a type

GlassBricks commented 8 months ago

Restricting the possible values of type would be possible.

Also, there is a ResourceEntity type (and for other subclasses), which contains only properties specific to a subclass. This might be what you are looking for. However, subclass types do not (yet) include prototype hierarchy relationships (e.g. an assembler is also an entity-with-force); it could be a good idea to add this in the future.

Dimava commented 8 months ago

I think this could use the SuperclassNameMap or something lol

GlassBricks commented 2 weeks ago

Turns out defines.prototypes provides the "subclass map" the whole time, and since factorio v1.1.110 it just got fully defined. Additionally, specific string types such as EntityType, ItemType, EquipmentType were added in a earlier (this package) version.

As such, since types for v1.1.110 were just released (in package v2.13.0), I'm closing this issue as "fixed".