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

Replace Record<K, V> with { [key: K]: V } #277

Merged
merged 3 commits into from
Mar 20, 2024
Merged

Conversation

escritorio-gustavo
Copy link
Contributor

Goal

Allow recursive HashMap types by changing their inlining from Record<K, V> to { [key: K]: V }
Closes #134

Changes

The impl block for HashMap was altered

Checklist

  • I have followed the steps listed in the Contributing guide.
  • If necessary, I have added documentation related to the changes made.
  • I have added or updated the tests related to the changes made.

ts-rs/src/lib.rs Outdated
@@ -863,15 +863,15 @@ impl<K: TS, V: TS, H> TS for HashMap<K, V, H> {
type WithoutGenerics = HashMap<Dummy, Dummy>;

fn ident() -> String {
"Record".to_owned()
"{ [key: any]: any }".to_owned()
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I actually have no idea what this method should return. Maybe it should panic!?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yep, it should panic. This method is only ever used if the type is derived

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think that's right!
We only use ident() for constructing Dependency, and we only do that for type which can be exported (which are those that are derived).

@NyxCode
Copy link
Collaborator

NyxCode commented Mar 20, 2024

Awesome, thank you!

While playing with this a bit, I found one potential edge-case:

type A<K, V> = { [key: K]: V }

which would be generated by struct Wrapper<K, V>(HashMap<K, V>) doesn't compile.

However, type A<K, V> = Record<K, V>; doesn't compile either, which is what we had before.

So right now, I can't think of any regression this would cause. Are there any edge-cases you're concerned about?

@NyxCode
Copy link
Collaborator

NyxCode commented Mar 20, 2024

As far as I can tell, I think this is a minor change (e.g 8.0.1), and shouldn't break anything.

NyxCode added a commit that referenced this pull request Mar 20, 2024
@escritorio-gustavo escritorio-gustavo merged commit a2feac5 into main Mar 20, 2024
14 checks passed
@escritorio-gustavo escritorio-gustavo deleted the replace_record branch March 20, 2024 17:13
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Use { [key: K]: V } instead or Record<K, V> for Maps
2 participants