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

#[derive(TS)] and associated types #261

Closed
WilsonGramer opened this issue Mar 9, 2024 · 14 comments · Fixed by #264
Closed

#[derive(TS)] and associated types #261

WilsonGramer opened this issue Mar 9, 2024 · 14 comments · Fixed by #264

Comments

@WilsonGramer
Copy link
Contributor

In the following code, #[derive(TS)] generates a T: TS bound even though T is never used, only its associated types:

trait MyTrait {
    type U: TS;
}

#[derive(TS)]
struct MyStruct<T: MyTrait> {
    u: T::U,
}

fn test() {
    struct X;

    impl MyTrait for X {
        type U = i32;
    }

    // error: the trait bound `test::X: ts_rs::TS` is not satisfied
    <MyStruct<X> as TS>::export();
}

I would expect the above example to compile because i32 implements TS.

It looks like the derive macro generates the following:

impl<T: MyTrait> ts_rs::TS for MyStruct<T>
where
    T: ts_rs::TS,
{
    ...
}

In general, instead of adding bounds for each type parameter, #[derive(TS)] could add bounds for each field:

#[derive(TS)]
struct S {
    a: A,
    b: B,
    c: C,
}

impl ts_rs::TS for S
where
    A: ts_rs::TS,
    B: ts_rs::TS,
    C: ts_rs::TS,
{
    ...
}
@WilsonGramer
Copy link
Contributor Author

Alternatively, #[ts] could support a bound attribute like serde: #[ts(bound = "")]

@NyxCode
Copy link
Collaborator

NyxCode commented Mar 10, 2024

Hey!
This is a known limitation (we should probably document that somewhere).

Besides the technical reasons we currently don't support this, it's also unclear to me what we should generate for types containing associated types of a trait.

The only thing we could do is let you export a concrete instance of MyStruct to a non-generic typescript type.
However, TS currently requires that we can just export the type without any additional information. If the type is generic, then we generate a generic TypeScript type.

In this example, it's the implementation of TS::decl() trying to generate a generic TypeScript definition that causes the compiler error you see. The function which would give you the concrete (non-generic) TypeScript definition is TS::decl_concrete(), but if the thing doesn't compile, that doesn't matter.

@NyxCode
Copy link
Collaborator

NyxCode commented Mar 10, 2024

If, at some point, we get const-generic &'static str on stable, something like this would work:

trait MyTrait {
    type U: TS;
}

#[derive(TS)]
struct MyStruct<T: MyTrait> {
    u: T::U,
}

impl<const NAME: &'static str> MyTrait for ts_rs::Dummy<NAME> { 
    type U = i32; 
}

Then, MyStruct::<Anything>::decl() would always yield

type MyStruct<T> { u: number };

no matter what Anything is.

@WilsonGramer
Copy link
Contributor Author

WilsonGramer commented Mar 10, 2024

@NyxCode That makes sense, thank you! What do you think about an attribute to ignore a generic parameter?

#[derive(TS)]
struct MyStruct<#[ts(skip)] T: MyTrait> {
    #[ts(as = "i32")]
    u: T::U,
}

For context, my use case is a compiler whose data structures are generic over a Driver:

trait Driver {
    type Info;
    ...
}

struct Expression<D: Driver> {
    info: D::Info,
    kind: ExpressionKind<D>,
}

enum ExpressionKind<D: Driver> {
    String(String),
    Number(i32),
    Call(Box<Expression<D>>, Vec<Expression<D>>),
    ...
}

But my TypeScript interface to this compiler always uses the same driver, so Info is always the same type (TsInfo) when the compiler is used via TypeScript. With the design above, I could use #[ts(skip)] on the D type parameter and #[ts(as = "TsInfo")] on the info field. That way, my Rust code can remain generic over the driver while my TypeScript API ignores the driver altogether.

Let me know what you think!

@NyxCode
Copy link
Collaborator

NyxCode commented Mar 10, 2024

I played with the idea of introducing #[ts(concrete = "SomeType")].
With it, your example would look like this:

trait Driver {
    type Info: TS; // (1)
}

struct TsDriver;
impl Driver for TsDriver {
    type Info = String;
}

#[derive(TS)]
struct Expression<#[ts(concrete = "TsDriver")] D: Driver> {
    info: D::Info,
}

(1): Ideally, this trait bound wouldn't be necessary, but it currently is.

I've put together a proof-of-concept here. There are a lot of edge-cases this just glosses over, but it's a start.
Could you give that branch a go and see if that's already enough for your use-case?

@WilsonGramer

This comment was marked as outdated.

@WilsonGramer
Copy link
Contributor Author

WilsonGramer commented Mar 10, 2024

@NyxCode Whoops – I forgot to add the #[ts(concrete)] attribute, haha! It works great with that!

use ts_rs::TS;

trait Driver {
    type Info;
}

#[derive(TS)]
struct TsInfo;

impl Driver for ts_rs::Dummy {
    type Info = TsInfo;
}

#[derive(TS)]
struct Expression<#[ts(concrete = "ts_rs::Dummy")] D: Driver> {
    #[ts(as = "TsInfo")]
    info: D::Info,
}

@WilsonGramer
Copy link
Contributor Author

WilsonGramer commented Mar 10, 2024

@NyxCode The only issue I'm getting now is when I add in serde — the ts attribute isn't being removed:

use serde::{Deserialize, Serialize};
use ts_rs::TS;

trait Driver {
    type Info: TS;
}

#[derive(TS)]
struct TsInfo;

impl Driver for ts_rs::Dummy {
    type Info = TsInfo;
}

#[derive(Debug, TS, Serialize, Deserialize)]
#[ts(export)]
struct Expression<#[ts(concrete = "ts_rs::Dummy")] D: Driver> {
    info: D::Info,
}
error: cannot find attribute `ts` in this scope
  --> src/lib.rs:17:21
   |
17 | struct Expression<#[ts(concrete = "ts_rs::Dummy")] D: Driver> {
   |                     ^^

Putting TS after Serialize, Deserialize doesn't seem to fix it either.

@NyxCode
Copy link
Collaborator

NyxCode commented Mar 10, 2024

I forgot to add the #[ts(concrete)] attribute, haha! It works great with that!

Awesome!

You could get rid of the #[ts(as = "..")] and put a trait bound on Driver::Info instead.
While in theory neither should be necessary, getting rid of that requirement would be pretty messy.

I also just pushed an other commit, and now #[ts(export)] works as expected.
While the branch definetely needs a lot of cleanup, it's nice to see that this could be a solution to this issue.

The only issue I'm getting now is when I add in serde — the ts attribute isn't being removed:

Oh, that's very interesting! No clue what's going on here, will have to look into that.

@NyxCode
Copy link
Collaborator

NyxCode commented Mar 10, 2024

Hm. Seems like serde is copying the #[ts(concrete = "..")] attributes over into it's impl Serialize. That's not good.

Unless there's some way to prevent that, we'll have to change the syntax, maybe to

#[derive(TS)]
#[ts(concrete(B = TsDriver)]
struct Expression<A, B: Driver> { }

@NyxCode
Copy link
Collaborator

NyxCode commented Mar 10, 2024

(serde#2710)

@WilsonGramer
Copy link
Contributor Author

@NyxCode Looks like changing the attribute to be on the struct instead of the generic parameters is the way to go — see dtolnay/syn#422.

@WilsonGramer
Copy link
Contributor Author

@NyxCode I just opened #262 to make the attribute be on the type instead of the generic parameters!

@NyxCode
Copy link
Collaborator

NyxCode commented Mar 13, 2024

FYI @WilsonGramer: in #264, we got rid of the : TS bound in

trait Driver {
    type Info: TS;
}

by generating smarter bounds on the impl.

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 a pull request may close this issue.

2 participants