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

Incorrect TS types generated from types containing type aliases #70

Closed
FruitieX opened this issue Dec 29, 2021 · 4 comments · Fixed by #233
Closed

Incorrect TS types generated from types containing type aliases #70

FruitieX opened this issue Dec 29, 2021 · 4 comments · Fixed by #233
Labels
bug Something isn't working difficulty: hard

Comments

@FruitieX
Copy link
Contributor

Minimal repro:

use std::collections::HashMap;
use ts_rs::TS;

type TypeAlias = HashMap<String, String>;

#[derive(TS)]
#[ts(export)]
enum Enum {
    A(TypeAlias),
    B(HashMap<String, String>),
}

#[derive(TS)]
#[ts(export)]
struct Struct {
    a: TypeAlias,
    b: HashMap<String, String>
}

fn main() {}

Generates:

export type Enum = { A: Record } | { B: Record<string, string> };
export interface Struct { a: Record, b: Record<string, string>, }

Expected both generated variants/fields to be identical.

@FruitieX FruitieX changed the title Bug when generating TS types from types containing type aliases Incorrect TS types generated from types containing type aliases Dec 29, 2021
@NyxCode NyxCode added bug Something isn't working difficulty: hard labels Jan 2, 2022
@NyxCode
Copy link
Collaborator

NyxCode commented Jan 2, 2022

The current processing of types with generic parameters breaks with type aliases since the generic parameters are not explicitly named in-place where the type is used.

@FruitieX
Copy link
Contributor Author

FruitieX commented Jan 3, 2022

A workaround for now is to just avoid type aliases in anything generic* that needs to be exported. Use newtype structs instead:

pub type MyType = HashMap<string, string>;

becomes

#[derive(TS)]
#[ts(export)]
pub struct MyType(pub HashMap<string, string>);

In addition you can now export TS bindings to these types as well. Of course you will have to adapt your code somewhat to support (un)wrapping these newtype structs.

@NyxCode
Copy link
Collaborator

NyxCode commented Jan 3, 2022

type aliases do work as long as the type they alias is not generic

@oscartbeaumont
Copy link

I have run across this same issue. Luckily I can workaround the issue by using wrapper types like what has been documented above.

I gave fixing the issue a shot and very quickly noticed I was way out of my depth, I have however created some tests which show the specific issue I had on my fork here. I thought I would post them in case they can help anyone.

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