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
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions ts-rs/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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).

}

fn name() -> String {
format!("Record<{}, {}>", K::name(), V::name())
format!("{{ [key: {}]: {} }}", K::name(), V::name())
}

fn inline() -> String {
format!("Record<{}, {}>", K::inline(), V::inline())
format!("{{ [key: {}]: {} }}", K::inline(), V::inline())
}

fn dependency_types() -> impl TypeList
Expand Down
2 changes: 1 addition & 1 deletion ts-rs/tests/generics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ fn test() {

assert_eq!(
Container::decl(),
"type Container = { foo: Generic<number>, bar: Array<Generic<number>>, baz: Record<string, Generic<string>>, };"
"type Container = { foo: Generic<number>, bar: Array<Generic<number>>, baz: { [key: string]: Generic<string> }, };"
);
}

Expand Down
6 changes: 3 additions & 3 deletions ts-rs/tests/hashmap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ struct Hashes {
fn hashmap() {
assert_eq!(
Hashes::decl(),
"type Hashes = { map: Record<string, string>, set: Array<string>, };"
"type Hashes = { map: { [key: string]: string }, set: Array<string>, };"
)
}

Expand All @@ -35,7 +35,7 @@ struct HashesHasher {
fn hashmap_with_custom_hasher() {
assert_eq!(
HashesHasher::decl(),
"type HashesHasher = { map: Record<string, string>, set: Array<string>, };"
"type HashesHasher = { map: { [key: string]: string }, set: Array<string>, };"
)
}

Expand Down Expand Up @@ -67,6 +67,6 @@ fn with_custom_types() {
);
assert_eq!(
HashMapWithCustomTypes::decl(),
"type HashMapWithCustomTypes = { map: Record<CustomKey, CustomValue>, };"
"type HashMapWithCustomTypes = { map: { [key: CustomKey]: CustomValue }, };"
);
}
2 changes: 1 addition & 1 deletion ts-rs/tests/indexmap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,6 @@ struct Indexes {
fn indexmap() {
assert_eq!(
Indexes::decl(),
"type Indexes = { map: Record<string, string>, set: Array<string>, };"
"type Indexes = { map: { [key: string]: string }, set: Array<string>, };"
)
}
4 changes: 2 additions & 2 deletions ts-rs/tests/issue-168.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,13 @@ fn issue_168() {
FooInlined::export_to_string().unwrap(),
"// This file was generated by [ts-rs](https://github.com/Aleph-Alpha/ts-rs). Do not edit this file manually.\n\
\n\
export type FooInlined = { map: Record<number, { map: Record<number, { map: Record<number, string>, }>, }>, };"
export type FooInlined = { map: { [key: number]: { map: { [key: number]: { map: { [key: number]: string }, } }, } }, };"
);
assert_eq!(
Foo::export_to_string().unwrap(),
"// This file was generated by [ts-rs](https://github.com/Aleph-Alpha/ts-rs). Do not edit this file manually.\n\
import type { Bar } from \"./Bar\";\n\
\n\
export type Foo = { map: Record<number, Bar>, };"
export type Foo = { map: { [key: number]: Bar }, };"
);
}
4 changes: 2 additions & 2 deletions ts-rs/tests/issue-70.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,11 @@ struct Struct {
fn issue_70() {
assert_eq!(
Enum::decl(),
"type Enum = { \"A\": Record<string, string> } | { \"B\": Record<string, string> };"
"type Enum = { \"A\": { [key: string]: string } } | { \"B\": { [key: string]: string } };"
);
assert_eq!(
Struct::decl(),
"type Struct = { a: Record<string, string>, b: Record<string, string>, };"
"type Struct = { a: { [key: string]: string }, b: { [key: string]: string }, };"
);
}

Expand Down
2 changes: 1 addition & 1 deletion ts-rs/tests/lifetimes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,6 @@ fn contains_borrow() {
fn contains_borrow_type_args() {
assert_eq!(
A::decl(),
"type A = { a: Array<number>, b: Array<B<number>>, c: Record<string, boolean>, };"
"type A = { a: Array<number>, b: Array<B<number>>, c: { [key: string]: boolean }, };"
);
}
14 changes: 7 additions & 7 deletions ts-rs/tests/self_referential.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,8 +83,8 @@ fn enum_externally_tagged() {
{ \"C\": E } | \
{ \"D\": E } | \
{ \"E\": [E, E, E, E] } | \
{ \"F\": { a: E, b: E, c: Record<string, E>, d: E | null, e?: E | null, f?: E, } } | \
{ \"G\": [Array<E>, Array<E>, Record<string, E>] };"
{ \"F\": { a: E, b: E, c: { [key: string]: E }, d: E | null, e?: E | null, f?: E, } } | \
{ \"G\": [Array<E>, Array<E>, { [key: string]: E }] };"
);
}

Expand Down Expand Up @@ -122,7 +122,7 @@ fn enum_internally_tagged() {
{ \"tag\": \"C\" } & I | \
{ \"tag\": \"D\" } & I | \
{ \"tag\": \"E\" } & Array<I> | \
{ \"tag\": \"F\", a: I, b: I, c: Record<I, I>, d: I | null, e?: I | null, f?: I, };"
{ \"tag\": \"F\", a: I, b: I, c: { [key: I]: I }, d: I | null, e?: I | null, f?: I, };"
);
}

Expand Down Expand Up @@ -170,7 +170,7 @@ fn enum_adjacently_tagged() {
\"content\": { \
a: A, \
b: A, \
c: Record<string, A>, \
c: { [key: string]: A }, \
d: A | null, \
e?: A | null, \
f?: A, \
Expand All @@ -179,9 +179,9 @@ fn enum_adjacently_tagged() {
{ \
\"tag\": \"G\", \
\"content\": [\
Array<A>, \
[A, A, A, A], \
Record<string, A>\
Array<A>, \
[A, A, A, A], \
{ [key: string]: A }\
] \
};"
);
Expand Down
Loading