diff --git a/crates/backend/src/typegen.rs b/crates/backend/src/typegen.rs index c9d4de08..40f3701d 100644 --- a/crates/backend/src/typegen.rs +++ b/crates/backend/src/typegen.rs @@ -263,6 +263,7 @@ fn is_ts_function_type_notation(ty: &Type) -> bool { } } +// return (type, is_optional, is_variadic) pub fn ty_to_ts_type( ty: &Type, is_return_ty: bool, diff --git a/crates/backend/src/typegen/struct.rs b/crates/backend/src/typegen/struct.rs index 0ac18d85..e6f3d1f5 100644 --- a/crates/backend/src/typegen/struct.rs +++ b/crates/backend/src/typegen/struct.rs @@ -36,13 +36,17 @@ 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; + let (resolved_type, is_optional, _) = ty_to_ts_type(output_type, false, true, false); t.borrow_mut().insert( self.name.to_string(), if resolved_type == "undefined" { "void".to_owned() } else { - resolved_type + if is_optional { + format!("{} | null", resolved_type) + } else { + resolved_type + } }, ); }); diff --git a/examples/napi/__tests__/__snapshots__/typegen.spec.ts.md b/examples/napi/__tests__/__snapshots__/typegen.spec.ts.md index 43e37706..4bb95143 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 asyncTaskOptionalReturn(): Promise␊ + ␊ export function asyncTaskVoidReturn(): Promise␊ ␊ export interface B {␊ diff --git a/examples/napi/__tests__/__snapshots__/typegen.spec.ts.snap b/examples/napi/__tests__/__snapshots__/typegen.spec.ts.snap index 522fda1a..30c3aa3f 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 782b9ca9..340c29e2 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 asyncTaskOptionalReturn(): Promise + export function asyncTaskVoidReturn(): Promise export interface B { diff --git a/examples/napi/src/task.rs b/examples/napi/src/task.rs index c4e9b050..0b23f986 100644 --- a/examples/napi/src/task.rs +++ b/examples/napi/src/task.rs @@ -49,3 +49,24 @@ impl Task for AsyncTaskVoidReturn { fn async_task_void_return() -> AsyncTask { AsyncTask::new(AsyncTaskVoidReturn {}) } + +struct AsyncTaskOptionalReturn {} + +#[napi] +impl Task for AsyncTaskOptionalReturn { + type JsValue = Option; + type Output = (); + + fn compute(&mut self) -> Result { + Ok(()) + } + + fn resolve(&mut self, _env: Env, _: Self::Output) -> Result { + Ok(None) + } +} + +#[napi] +fn async_task_optional_return() -> AsyncTask { + AsyncTask::new(AsyncTaskOptionalReturn {}) +}