diff --git a/crates/backend/src/typegen.rs b/crates/backend/src/typegen.rs index 344351d7..d0a2357c 100644 --- a/crates/backend/src/typegen.rs +++ b/crates/backend/src/typegen.rs @@ -351,6 +351,10 @@ pub fn ty_to_ts_type(ty: &Type, is_return_ty: bool, is_struct_field: bool) -> (S ts_ty.unwrap_or_else(|| ("any".to_owned(), false)) } Type::Group(g) => ty_to_ts_type(&g.elem, is_return_ty, is_struct_field), + Type::Array(a) => { + let (element_type, is_optional) = ty_to_ts_type(&a.elem, is_return_ty, is_struct_field); + (format!("{}[]", element_type), is_optional) + } _ => ("any".to_owned(), false), } } diff --git a/crates/napi/src/bindgen_runtime/js_values/array.rs b/crates/napi/src/bindgen_runtime/js_values/array.rs index 31fdaf63..96100b31 100644 --- a/crates/napi/src/bindgen_runtime/js_values/array.rs +++ b/crates/napi/src/bindgen_runtime/js_values/array.rs @@ -177,6 +177,36 @@ impl TypeName for Vec { } } +impl ToNapiValue for [T; N] +where + T: ToNapiValue + Copy, +{ + unsafe fn to_napi_value(env: sys::napi_env, val: Self) -> Result { + let mut arr = Array::new(env, val.len() as u32)?; + + for (i, v) in val.into_iter().enumerate() { + arr.set(i as u32, v)?; + } + + unsafe { Array::to_napi_value(env, arr) } + } +} + +impl ToNapiValue for &[T] +where + T: ToNapiValue + Copy, +{ + unsafe fn to_napi_value(env: sys::napi_env, val: Self) -> Result { + let mut arr = Array::new(env, val.len() as u32)?; + + for (i, v) in val.iter().enumerate() { + arr.set(i as u32, *v)?; + } + + unsafe { Array::to_napi_value(env, arr) } + } +} + impl ToNapiValue for Vec where T: ToNapiValue, diff --git a/examples/napi/__test__/typegen.spec.ts.md b/examples/napi/__test__/typegen.spec.ts.md index ef2b65cc..bfa3b602 100644 --- a/examples/napi/__test__/typegen.spec.ts.md +++ b/examples/napi/__test__/typegen.spec.ts.md @@ -26,6 +26,8 @@ Generated by [AVA](https://avajs.dev). export function getNums(): Array␊ export function sumNums(nums: Array): number␊ export function toJsObj(): object␊ + export function getNumArr(): number[]␊ + export function getNestedNumArr(): number[][][]␊ export function readFileAsync(path: string): Promise␊ export function asyncMultiTwo(arg: number): Promise␊ export function bigintAdd(a: bigint, b: bigint): bigint␊ diff --git a/examples/napi/__test__/typegen.spec.ts.snap b/examples/napi/__test__/typegen.spec.ts.snap index 3068b74c..8a9b5be2 100644 Binary files a/examples/napi/__test__/typegen.spec.ts.snap and b/examples/napi/__test__/typegen.spec.ts.snap differ diff --git a/examples/napi/__test__/values.spec.ts b/examples/napi/__test__/values.spec.ts index 5b21a020..459e4c68 100644 --- a/examples/napi/__test__/values.spec.ts +++ b/examples/napi/__test__/values.spec.ts @@ -105,6 +105,8 @@ import { AnotherClassForEither, receiveDifferentClass, useTokioWithoutAsync, + getNumArr, + getNestedNumArr, } from '../' test('export const', (t) => { @@ -144,6 +146,8 @@ test('array', (t) => { t.deepEqual(getWords(), ['foo', 'bar']) t.is(sumNums([1, 2, 3, 4, 5]), 15) + t.deepEqual(getNumArr(), [1, 2]) + t.deepEqual(getNestedNumArr(), [[[1]], [[1]]]) }) test('map', (t) => { diff --git a/examples/napi/index.d.ts b/examples/napi/index.d.ts index bfbd2069..f653f1bb 100644 --- a/examples/napi/index.d.ts +++ b/examples/napi/index.d.ts @@ -16,6 +16,8 @@ export function getWords(): Array export function getNums(): Array export function sumNums(nums: Array): number export function toJsObj(): object +export function getNumArr(): number[] +export function getNestedNumArr(): number[][][] export function readFileAsync(path: string): Promise export function asyncMultiTwo(arg: number): Promise export function bigintAdd(a: bigint, b: bigint): bigint diff --git a/examples/napi/src/array.rs b/examples/napi/src/array.rs index bec01a49..1ba32d80 100644 --- a/examples/napi/src/array.rs +++ b/examples/napi/src/array.rs @@ -23,3 +23,13 @@ fn to_js_obj(env: Env) -> napi::Result { arr.insert(42)?; arr.coerce_to_object() } + +#[napi] +fn get_num_arr() -> [u32; 2] { + [1, 2] +} + +#[napi] +fn get_nested_num_arr() -> [[[u32; 1]; 1]; 2] { + [[[1]], [[1]]] +}