Types Overwrite

Types Overwrite

In most cases, NAPI-RS will generate the right TypeScript types for you. But in some scenarios, you may want to overwrite the arguments or return type.

ThreadsafeFunction is an example, because the ThreadsafeFunction is too complex, NAPI-RS can't generate the right TypeScript types for it. You always need to overwrite its argument type.


Rewrite the arguments type of the function, NAPI-RS will put the rewritten type into the brace of the function signature.

#[napi(ts_args_type="callback: (err: null | Error, result: number) => void")]
fn call_threadsafe_function(callback: JsFunction) -> Result<()> {
  let tsfn: ThreadsafeFunction<u32, ErrorStrategy::CalleeHandled> = callback
    .create_threadsafe_function(0, |ctx| {
      ctx.env.create_uint32(ctx.value + 1).map(|v| vec![v])
  for n in 0..100 {
    let tsfn = tsfn.clone();
    thread::spawn(move || {, ThreadsafeFunctionCallMode::Blocking);


export function callThreadsafeFunction(
  callback: (err: null | Error, result: number) => void,
): void


Rewrite one or more argument types of a function individually, NAPI-RS will put the rewritten types into the brace of the function signature and will auto-derive the other ones.

fn override_individual_arg_on_function(
  not_overridden: String,
  #[napi(ts_arg_type = "() => string")] f: JsFunction,
  not_overridden2: u32,
) {
// code ...
export function overrideIndividualArgOnFunction(
  notOverridden: string,
  f: () => string,
  notOverridden2: number,
): string


Rewrite the return type of the function, NAPI-RS will add the rewritten type to the end of the function signature.

fn return_something_unknown(env: Env) -> Result<JsUnknown> {
  env.create_uint32(42).map(|v| v.into_unknown())
export function returnSomethingUnknown(): number


Overwrite the generated ts-type of a field in a struct.

pub struct TsTypeChanged {
  #[napi(ts_type = "MySpecialString")]
  pub type_override: String,

  #[napi(ts_type = "object")]
  pub type_override_optional: Option<String>,
export interface TsTypeChanged {
  typeOverride: MySpecialString
  typeOverrideOptional?: object