diff --git a/crates/backend/src/typegen/struct.rs b/crates/backend/src/typegen/struct.rs index 971edfd2..0ac18d85 100644 --- a/crates/backend/src/typegen/struct.rs +++ b/crates/backend/src/typegen/struct.rs @@ -36,9 +36,14 @@ impl ToTypeDef for NapiImpl { fn to_type_def(&self) -> Option { if let Some(output_type) = &self.task_output_type { TASK_STRUCTS.with(|t| { + let resolved_type = ty_to_ts_type(output_type, false, true, false).0; t.borrow_mut().insert( self.name.to_string(), - ty_to_ts_type(output_type, false, true, false).0, + if resolved_type == "undefined" { + "void".to_owned() + } else { + resolved_type + }, ); }); } diff --git a/crates/macro/src/parser/mod.rs b/crates/macro/src/parser/mod.rs index 86afc1f0..e58f62ba 100644 --- a/crates/macro/src/parser/mod.rs +++ b/crates/macro/src/parser/mod.rs @@ -1033,9 +1033,7 @@ impl ConvertToAST for syn::ItemImpl { if let Some((_, t, _)) = &self.trait_ { if let Some(PathSegment { ident, .. }) = t.segments.last() { if ident == "Task" && m.ident == "JsValue" { - if let Type::Path(_) = &m.ty { - task_output_type = Some(m.ty.clone()); - } + task_output_type = Some(m.ty.clone()); } else if ident == "Generator" { if let Type::Path(_) = &m.ty { if m.ident == "Yield" { diff --git a/examples/napi/__tests__/__snapshots__/typegen.spec.ts.md b/examples/napi/__tests__/__snapshots__/typegen.spec.ts.md index 45c24471..43e37706 100644 --- a/examples/napi/__tests__/__snapshots__/typegen.spec.ts.md +++ b/examples/napi/__tests__/__snapshots__/typegen.spec.ts.md @@ -246,6 +246,8 @@ Generated by [AVA](https://avajs.dev). ␊ export function asyncReduceBuffer(buf: Buffer): Promise␊ ␊ + export function asyncTaskVoidReturn(): Promise␊ + ␊ export interface B {␊ bar: number␊ }␊ diff --git a/examples/napi/__tests__/__snapshots__/typegen.spec.ts.snap b/examples/napi/__tests__/__snapshots__/typegen.spec.ts.snap index 3801ad87..522fda1a 100644 Binary files a/examples/napi/__tests__/__snapshots__/typegen.spec.ts.snap and b/examples/napi/__tests__/__snapshots__/typegen.spec.ts.snap differ diff --git a/examples/napi/index.d.ts b/examples/napi/index.d.ts index bd950724..782b9ca9 100644 --- a/examples/napi/index.d.ts +++ b/examples/napi/index.d.ts @@ -236,6 +236,8 @@ export function asyncPlus100(p: Promise): Promise export function asyncReduceBuffer(buf: Buffer): Promise +export function asyncTaskVoidReturn(): Promise + export interface B { bar: number } diff --git a/examples/napi/src/task.rs b/examples/napi/src/task.rs index b2f1c4f1..c4e9b050 100644 --- a/examples/napi/src/task.rs +++ b/examples/napi/src/task.rs @@ -28,3 +28,24 @@ fn without_abort_controller(a: u32, b: u32) -> AsyncTask { fn with_abort_controller(a: u32, b: u32, signal: AbortSignal) -> AsyncTask { AsyncTask::with_signal(DelaySum(a, b), signal) } + +struct AsyncTaskVoidReturn {} + +#[napi] +impl Task for AsyncTaskVoidReturn { + type JsValue = (); + type Output = (); + + fn compute(&mut self) -> Result { + Ok(()) + } + + fn resolve(&mut self, _env: Env, output: Self::Output) -> Result { + Ok(output) + } +} + +#[napi] +fn async_task_void_return() -> AsyncTask { + AsyncTask::new(AsyncTaskVoidReturn {}) +}