diff --git a/crates/backend/src/ast.rs b/crates/backend/src/ast.rs index 140c0e26..c832ce3f 100644 --- a/crates/backend/src/ast.rs +++ b/crates/backend/src/ast.rs @@ -16,6 +16,7 @@ pub struct NapiFn { pub parent: Option, pub strict: bool, pub js_mod: Option, + pub ts_generic_types: Option, pub ts_args_type: Option, pub ts_return_type: Option, pub skip_typescript: bool, diff --git a/crates/backend/src/typegen/fn.rs b/crates/backend/src/typegen/fn.rs index 03715e7c..b1b2133d 100644 --- a/crates/backend/src/typegen/fn.rs +++ b/crates/backend/src/typegen/fn.rs @@ -59,9 +59,14 @@ impl ToTypeDef for NapiFn { } let def = format!( - r#"{prefix} {name}({args}){ret}"#, + r#"{prefix} {name}{generic}({args}){ret}"#, prefix = self.gen_ts_func_prefix(), name = &self.js_name, + generic = &self + .ts_generic_types + .as_ref() + .map(|g| format!("<{}>", g)) + .unwrap_or("".to_string()), args = self .ts_args_type .clone() diff --git a/crates/macro/src/parser/attrs.rs b/crates/macro/src/parser/attrs.rs index e066ed6c..aaa5cd20 100644 --- a/crates/macro/src/parser/attrs.rs +++ b/crates/macro/src/parser/attrs.rs @@ -55,6 +55,7 @@ macro_rules! attrgen { (ts_args_type, TsArgsType(Span, String, Span)), (ts_return_type, TsReturnType(Span, String, Span)), (ts_type, TsType(Span, String, Span)), + (ts_generic_types, TsGenericTypes(Span, String, Span)), // impl later // (inspectable, Inspectable(Span)), diff --git a/crates/macro/src/parser/mod.rs b/crates/macro/src/parser/mod.rs index 97c180f4..53947c19 100644 --- a/crates/macro/src/parser/mod.rs +++ b/crates/macro/src/parser/mod.rs @@ -577,6 +577,7 @@ fn napi_fn_from_decl( attrs, strict: opts.strict().is_some(), js_mod: opts.namespace().map(|(m, _)| m.to_owned()), + ts_generic_types: opts.ts_generic_types().map(|(m, _)| m.to_owned()), ts_args_type: opts.ts_args_type().map(|(m, _)| m.to_owned()), ts_return_type: opts.ts_return_type().map(|(m, _)| m.to_owned()), skip_typescript: opts.skip_typescript().is_some(),