-
Notifications
You must be signed in to change notification settings - Fork 116
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
Feature request: allow method of including [propName: string]: any
to type
#335
Comments
Hey! |
We do not support generating these "indexable" types, since there is no real rust equivalent. If you really need to have a type Item = ItemData & { [propName: string]: any }; You could also force ts-rs to generate that for you: #[derive(ts_rs::TS)]
#[ts(export)]
struct Item {
id: String,
#[ts(flatten)]
props: Props,
}
struct Props;
impl ts_rs::TS for Props {
type WithoutGenerics = Self;
fn decl() -> String { unreachable!() }
fn decl_concrete() -> String { unreachable!() }
fn name() -> String { unreachable!() }
fn inline() -> String { unreachable!() }
fn inline_flattened() -> String { "{ [propName: string]: any }".to_owned() }
} But this is really abusing the library. |
type Item<D> = { name: string, /* ... */ } & D; If this what you'd like it to generate? |
[propName: string]: any
to type
basically, what i'm trying to achieve is a type that defines an object with a few set fields, and then any field after that. for example, here's what a JSON representation of an Item would look like: {
"__sc_id": "YFCzT4NxQEjYxjBvO_zCc",
"__sc_created_at": "2024-06-27T20:23:30.217589619Z",
"__sc_modified_at": "2024-06-27T20:23:30.217589619Z",
"__sc_published_at": null,
"hi": "hello world!",
"number": 1,
"test": {
"type": "Unit"
}
} and here's how the D (a struct called Test) is defined: struct Test {
pub hi: String,
pub number: i32,
#[field(validate)]
pub test: TestEnum,
}
#[doc_enum]
#[derive(Clone)]
enum TestEnum {
Unit,
Struct { eeee: String },
} the reason i asked for the specific application this is for is a cms, where the shape of the user's data isn't known at type generation time, and the type safety's guartunteed elsewhere by a schema edit: typos and formatting |
I see! Please let me know if that fits your use-case. If not, and you actually do need to allow for arbitrary additional fields using |
i'm not fully convinced the generics flattening fit in my use case. the problem is that the concrete type will be provided by the end users, and i don't think it's a good idea to force them to derive (then export) the TS type to make them useful in the editor i'm working on. i do think that doing the hack you offered would be OK, because my usecase is ultra-specific and definitely comes with a healthy amount of "i know what i'm doing". whether or not you want to make a more elegant way to achieve that is up to you. |
I wouldn't even call it a hack, if the issue is having a generic type that is unknown to Rust be flattened and let TS figure out the generic, having |
That's true! I suspect having a |
Anyway, If you do go with |
is there a good way to address the trait bound TS wants for the generic? right now i'm getting this compiler error, which runs up against me not wanting to force end users to generate typescript types
|
There's |
#[derive(ts_rs::TS, serde::Serialize)]
#[ts(export, concrete(D = AnythingElse))]
struct Item<D> {
id: String,
#[serde(flatten)]
inner: D,
}
struct AnythingElse;
impl ts_rs::TS for AnythingElse {
type WithoutGenerics = Self;
fn decl() -> String { unreachable!() }
fn decl_concrete() -> String { unreachable!() }
fn name() -> String { unreachable!() }
fn inline() -> String { unreachable!() }
fn inline_flattened() -> String { "{ [propName: string]: any }".to_owned() }
} this here should should just work as-is |
yeah that suggestion works, if you want, you could include the type in ts_rs as IndexibleAny or something, with a stern warning in the docs saying why in most cases it's probably a bad idea |
You could add a cargo feature to your library that gates the use of |
Great! I'd rather not include this, since it's really not something I'd recommend. GitHub issues are nicely searchable, so if someone stumbles across this, I hope they find this issue. |
i have a struct with a generic i'm exporting as a typescript type, however due to this struct being a part of a library where the generic will be provided by the user, there's not really a good way to define a type. since it's flattened, what i'd want to do is include a
[propName: string]: any
to the end like the typescript docs recommend, but i don't see a clean way to do that. here's the closest i've been able to get:The text was updated successfully, but these errors were encountered: