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

bug: Incorrect ts generation when generating HashMaps that enums as a key. #349

Closed
MrVintage710 opened this issue Aug 25, 2024 · 2 comments
Labels
bug Something isn't working

Comments

@MrVintage710
Copy link

Describe the bug
While is was working with this library I stumbled in to, as far as I could tell, breaking behavior. I had a setup similar to the following:

#[derive(TS, Hash, Eq, PartialEq)]
pub enum ComponentId {
  name,
  bulk
}

#[derive(TS)]
pub struct Entity {
  components : HashMap<ComponentId, Component>
}

When I translate ComponentId I get the expected output:

export type ComponentId = "name" | "bulk";

However when generating the ts file for Entity I get the following:

import type { Component } from "./Component";
import type { ComponentId } from "./ComponentId";

export type Entity = { components: { [key: ComponentId]: Component }, };

This throws an error as it is not the correct format. Since ComponentId is a literal type, the correct translation should be the following:

export type Entity = { components: { [key in ComponentId]: Component }, };

Notice that after key, the colon should be the in keyword. Is there away for ts-rs to be aware of this in the future? For know I just have a post processing step that makes that change, but it seems like this behavior is not intentional.

To Reproduce
Steps to reproduce the behavior:

  1. Go to 'main.rs'
  2. Create the following types: 'enum ComponentId { name, bulk }', enum Component { foo, bar}, struct Entity { components : HashMap<ComponentId, Component> }
  3. Derive/implement TS for all types made
  4. See error in generated ts

Expected behavior
When generating the types listed above, Entity should generate the following:

export type Entity = { components: { [key in ComponentId]: Component }, };

Version

ts-rs = "9.0.1"
@MrVintage710 MrVintage710 added the bug Something isn't working label Aug 25, 2024
@NyxCode
Copy link
Collaborator

NyxCode commented Aug 25, 2024

We fixed this in #339. Can you try the latest master?

@MrVintage710
Copy link
Author

This worked for me! Sorry I didnt see this change when I was looking around. Thanks for helping me out!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants