slint-ui/slint · error · napi::Error

expect Number, got

Error message

expect Number, got: {vt:?}

What it means

expect_number in the Node.js interpreter binding converts a Slint value to a JS number. When the underlying slint Interpreter Value's type is not Number, it throws a napi Error with status NumberExpected including the actual ValueType. This guards the JS<->Slint boundary so JS callers never receive silently-coerced garbage.

Solutions

  1. Check the value's type before conversion (inspect the .slint property declaration or the returned ValueType) and only call to_value on numeric values.
  2. Fix the .slint declaration so the property is an `int`/`float` if a number was intended.
  3. In JS, handle the thrown NumberExpected error and fall back to string/boolean conversion paths as appropriate.

Example fix

// before
const n = slintValueToJs(anyValue); // throws if not number
// after
if (typeof raw === 'number' || isNumericSlintType(raw)) {
  const n = slintValueToJs(raw);
}
Defensive patterns

Strategy: type-guard

Validate before calling

function isSlintNumber(v) {
  return v != null && (typeof v === 'number' || slintValueType(v) === 'number');
}

Type guard

const isNumberValue = (v) => slintValueType(v) === 'Number';

Try / catch

try {
  const n = toValue(value);
} catch (e) {
  if (e.code === napi.Status.NumberExpected) {
    // handle non-numeric value
  } else throw e;
}

Prevention

When it happens

Trigger: Converting a Slint Value to a JS value via to_value when the value is a string, bool, brush, struct, model, or image instead of a numeric (int/float) value; a .slint property declared as e.g. `string` being read as if numeric.

Common situations: Property type changed in the .slint file but JS code not updated; reading a property whose type was inferred differently; passing a Slint struct field of the wrong type through the interpreter bridge.

Understand the failure class

Background: Type mismatch errors: IllegalArgumentException, TypeError and type guards across 150 open-source libraries — this error's family across 150 libraries.

Related errors


AI-assisted analysis of slint-ui/slint@bb937076de (2026-09-16). Data as JSON: /api/errors/89358d0c3712dd8f. Report an issue: GitHub.

Appendix: source

Thrown at api/node/rust/interpreter/value.rs:33

use slint_interpreter::Value;
use smol_str::SmolStr;

/// A dynamic-length argument list for calling JS functions with a variable
/// number of arguments. Implements `JsValuesTupleIntoVec` so it can be used
/// directly with `Function::call`.
pub struct DynArgs(pub Vec<napi::sys::napi_value>);

impl JsValuesTupleIntoVec for DynArgs {
    fn into_vec(self, _env: napi::sys::napi_env) -> Result<Vec<napi::sys::napi_value>> {
        Ok(self.0)
    }
}

/// Safely extract a f64 from an Unknown, failing if the type is wrong.
fn expect_number(unknown: Unknown<'_>) -> Result<f64> {
    match unknown.get_type()? {
        ValueType::Number => unknown.coerce_to_number()?.get_double(),
        vt => Err(napi::Error::new(
            napi::Status::NumberExpected,
            format!("expect Number, got: {vt:?}"),
        )),
    }
}

/// Safely extract a String from an Unknown, failing if the type is wrong.
fn expect_string(unknown: Unknown<'_>) -> Result<String> {
    match unknown.get_type()? {
        ValueType::String => Ok(unknown.coerce_to_string()?.into_utf8()?.as_str()?.to_owned()),
        vt => Err(napi::Error::new(
            napi::Status::StringExpected,
            format!("expect String, got: {vt:?}"),
        )),
    }
}

/// Safely extract a bool from an Unknown, failing if the type is wrong.

View on GitHub (pinned to bb937076de)