Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions crates/ubrn_bindgen/src/bindings/gen_typescript/filters.rs
Original file line number Diff line number Diff line change
Expand Up @@ -203,3 +203,19 @@ pub fn docstring(docstring: &str, spaces: &i32) -> Result<String, askama::Error>
let spaces = usize::try_from(*spaces).unwrap_or_default();
Ok(textwrap::indent(&wrapped, &" ".repeat(spaces)))
}

pub(super) fn is_optional_type(
as_type: &impl AsType,
types: &TypeRenderer,
) -> Result<bool, askama::Error> {
let type_ = types.as_type(as_type);
Ok(matches!(type_, Type::Optional { .. }))
}

pub(super) fn type_name_without_undefined(
as_type: &impl AsType,
types: &TypeRenderer,
) -> Result<String, askama::Error> {
let og_type_name = type_name(as_type, types)?;
Ok(og_type_name.replace(" | undefined", ""))
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
export type {{ type_name }} = {
{%- for field in rec.fields() %}
{%- call ts::docstring(field, 4) %}
{{ field.name()|var_name }}: {{ field|type_name(self) }}
{{ field.name()|var_name }}{% if field|is_optional_type(self) %}?{% endif %}: {{ field|type_name_without_undefined(self) }}
{%- if !loop.last %},{% endif %}
{%- endfor %}
}
Expand Down
Loading