ts-rs icon indicating copy to clipboard operation
ts-rs copied to clipboard

Feature request: add an attribute to allow for types defined in ts only

Open legomind opened this issue 4 months ago • 3 comments

I am retriving records from a database, and one or more of the columns is a json datatype stored in a single column. I represent this using the type JsonValue, which wraps serde_json::Value.

Right now, the following works just fine, but results in the SomeStruct type having a field with an unknown type.

pub struct SomeStruct {
    #[ts(type="unknown")]
    pub questionare_results: JsonValue,
}

#[repr(transparent)]
#[derive(Clone, Debug, Default, PartialEq, Deserialize, Serialize)]
pub struct JsonValue(pub serde_json::Value);
export type SomeStruct = { questionare_results: unknown, };

Somthing like this would be ideal:

pub struct SomeStruct {
    #[ts(type="QuestionareResults", importedFrom="../QuestionareResults.ts")]
    pub questionare_results: JsonValue,
}
import type { QuestionareResults } from "../QuestionareResults.ts"
export type SomeStruct = { questionare_results: QuestionareResults, };

Perhaps a default import as well?

pub struct SomeStruct {
    #[ts(defaultType="QuestionareResults", importedFrom="../QuestionareResults.ts")]
    pub questionare_results: JsonValue,
}
import type QuestionareResults from "../QuestionareResults.ts"
export type SomeStruct = { questionare_results: QuestionareResults, };

legomind avatar Aug 16 '25 16:08 legomind

Hey @legomind, this is an interesting idea! We can't use #[ts(type = "...")] for this since we use that for type overrides, but we could make a #[ts(import(type = "QuestionareResults", from = "../QuestionareResults"))] or something like that

gustavo-shigueo avatar Aug 22 '25 11:08 gustavo-shigueo

One caveat is that I don't think our import logic handles default imports, since all our exports are named, so you might need to export QuestionareResults as a named export

gustavo-shigueo avatar Aug 22 '25 11:08 gustavo-shigueo

The issue of external types has come up a few times. My favorite approch so far has been #[ts(type = "import('./somewhere').SomeThing")]. While I am not against adding better support for these usecases, I havent seen anything more elegant than this.

NyxCode avatar Aug 22 '25 12:08 NyxCode