Comments (14)
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.
from ts-rs.
from ts-rs.
Alternatively, #[ts]
could support a bound
attribute like serde: #[ts(bound = "")]
from ts-rs.
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.
from ts-rs.
@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!
from ts-rs.
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?
from ts-rs.
@NyxCode This is pretty much what I need! The only issue is that as of this change, ts-rs generates a private struct for the type parameter that I can't implement the trait for, so I get this error:
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<D: Driver> {
#[ts(as = "TsInfo")]
info: D::Info,
}
error[E0277]: the trait bound `<Expression<D> as ts_rs::TS>::decl::D: Driver` is not satisfied
--> src/lib.rs:14:10
|
14 | #[derive(TS)]
| ^^ the trait `Driver` is not implemented for `<Expression<D> as ts_rs::TS>::decl::D`
|
= help: the trait `Driver` is implemented for `ts_rs::Dummy`
note: required by a bound in `Expression`
--> src/lib.rs:15:22
|
15 | struct Expression<D: Driver> {
| ^^^^^^ required by this bound in `Expression`
= note: this error originates in the derive macro `TS` (in Nightly builds, run with -Z macro-backtrace for more info)
Would removing the trait bounds on that generated struct fix the issue?
from ts-rs.
@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,
}
from ts-rs.
@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.
from ts-rs.
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.
from ts-rs.
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> { }
from ts-rs.
@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.
from ts-rs.
@NyxCode I just opened #262 to make the attribute be on the type instead of the generic parameters!
from ts-rs.
FYI @WilsonGramer: in #264, we got rid of the : TS
bound in
trait Driver {
type Info: TS;
}
by generating smarter bounds on the impl
.
from ts-rs.
Related Issues (20)
- bug: `impl Trait` only allowed in function and inherent method return types, not in trait method return types HOT 4
- TypeList recursion limit HOT 6
- Feature request: Export in the same file HOT 4
- bug: Using `#[ts(rename_all = "...")]` `snake_case`, `SCREAMING_SNAKE_CASE` and `kebab-case` is not compatible with serde
- Randomly failing `imports` test HOT 1
- Unqualified call to `Self::name()`
- `usize` and `isize` should be typed as `bigint` on 64-bit architectures HOT 6
- Feature request: `serde-wasm-bindgen` compatibility. HOT 5
- Feature request: How to export multiple types to the same file HOT 4
- Feature request: Typed array types HOT 3
- bug: Tuples cause imports to be missed HOT 5
- bug: files are missing final newlines
- bug: `export::path:absolute` function fails when `TS_RS_EXPORT_DIR` is set to an absolute path and the `path_bug` test is executed
- bug: TS generated tests fail on struct with `serde_json::Value` type when running `cargo test`. HOT 2
- `ts(optional)` should be working on any which has `skip_serializing_if` HOT 1
- Add all of `ts(function)` to docs. HOT 2
- publish release please HOT 5
- Use std::path::absolute HOT 2
- Feature request: allow method of including `[propName: string]: any` to type HOT 15
- Question to recent change `Records<K, V>` to `{ [key: K]: V }`
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from ts-rs.