Skip to content

bug: Auto Genric Bounds failing for #[ts(optional)] #453

@sinder38

Description

@sinder38

Describe the bug
Optional generic fields break with #[ts(optional)] if they are second generic or don't have implicit TS trait.
Please look at examples, they show it clearly.

To Reproduce

// Will break:
#[derive(Serialize, TS)]
#[ts(export, export_to = "optional_field/")]
struct GenericOptionalStruct<T1, T2>
{
	#[ts(optional)] // <- error is happening here
	a: Option<T2>,
	#[ts(optional = nullable)]
	b: Option<T1>,
	c: Option<T1>,
}

// Works:
#[derive(Serialize, TS)]
#[ts(export, export_to = "optional_field/")]
struct GenericOptionalStruct<T1, T2>
{
	#[ts(optional = nullable)] // <- set to nullable
	a: Option<T2>,
	#[ts(optional = nullable)]
	b: Option<T1>,
	c: Option<T1>,
}

// Works:
#[derive(Serialize, TS)]
#[ts(export, export_to = "optional_field/")]
struct GenericOptionalStruct<T1, T2>
where   // <- added TS requirements
	T1: TS,
	T2: TS,
{
	#[ts(optional)]
	a: Option<T2>,
	#[ts(optional = nullable)]
	b: Option<T1>,
	c: Option<T1>,
}

// Works: 
#[derive(Serialize, TS)]
#[ts(export, export_to = "optional_field/")]
struct GenericOptionalStruct<T> // <- removed T2
where 
	T: TS,
{
	#[ts(optional)]
	a: Option<T>,
	#[ts(optional = nullable)]
	b: Option<T>,
	c: Option<T>,
}

Expected behavior
For the first example to work

Version
11.1.0

Additional context
I haven't found out why exactly the issue is happening. may add later.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions