fix(napi-derive): async task optional output type (#1796)
This commit is contained in:
parent
938f4df83d
commit
e930a6aab3
6 changed files with 32 additions and 2 deletions
|
@ -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,
|
||||
|
|
|
@ -36,13 +36,17 @@ impl ToTypeDef for NapiImpl {
|
|||
fn to_type_def(&self) -> Option<TypeDef> {
|
||||
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 {
|
||||
if is_optional {
|
||||
format!("{} | null", resolved_type)
|
||||
} else {
|
||||
resolved_type
|
||||
}
|
||||
},
|
||||
);
|
||||
});
|
||||
|
|
|
@ -246,6 +246,8 @@ Generated by [AVA](https://avajs.dev).
|
|||
␊
|
||||
export function asyncReduceBuffer(buf: Buffer): Promise<number>␊
|
||||
␊
|
||||
export function asyncTaskOptionalReturn(): Promise<number | null>␊
|
||||
␊
|
||||
export function asyncTaskVoidReturn(): Promise<void>␊
|
||||
␊
|
||||
export interface B {␊
|
||||
|
|
Binary file not shown.
2
examples/napi/index.d.ts
vendored
2
examples/napi/index.d.ts
vendored
|
@ -236,6 +236,8 @@ export function asyncPlus100(p: Promise<number>): Promise<number>
|
|||
|
||||
export function asyncReduceBuffer(buf: Buffer): Promise<number>
|
||||
|
||||
export function asyncTaskOptionalReturn(): Promise<number | null>
|
||||
|
||||
export function asyncTaskVoidReturn(): Promise<void>
|
||||
|
||||
export interface B {
|
||||
|
|
|
@ -49,3 +49,24 @@ impl Task for AsyncTaskVoidReturn {
|
|||
fn async_task_void_return() -> AsyncTask<AsyncTaskVoidReturn> {
|
||||
AsyncTask::new(AsyncTaskVoidReturn {})
|
||||
}
|
||||
|
||||
struct AsyncTaskOptionalReturn {}
|
||||
|
||||
#[napi]
|
||||
impl Task for AsyncTaskOptionalReturn {
|
||||
type JsValue = Option<u32>;
|
||||
type Output = ();
|
||||
|
||||
fn compute(&mut self) -> Result<Self::Output> {
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn resolve(&mut self, _env: Env, _: Self::Output) -> Result<Self::JsValue> {
|
||||
Ok(None)
|
||||
}
|
||||
}
|
||||
|
||||
#[napi]
|
||||
fn async_task_optional_return() -> AsyncTask<AsyncTaskOptionalReturn> {
|
||||
AsyncTask::new(AsyncTaskOptionalReturn {})
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue