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

expect String, got

Error message

expect String, got: {vt:?}

What it means

expect_string in the Node.js interpreter binding extracts a JS string from a Slint Value. If the Value's type is not String, it throws a napi Error with status StringExpected and the actual ValueType in the message. It exists to fail fast rather than coerce unexpected types across the FFI boundary.

Solutions

  1. Verify the Slint value is actually a string (check the .slint property type) before converting.
  2. Correct the .slint declaration to `string` if the value should be textual.
  3. Add a type check in JS and route non-string values through the appropriate converter (expect_number/expect_bool).

Example fix

// before
const s = expect_string(value);
// after
const s = typeof value === 'string' ? value : String(coerceSlintValue(value));
Defensive patterns

Strategy: type-guard

Validate before calling

function isSlintString(v) {
  return typeof v === 'string' || slintValueType(v) === 'string';
}

Type guard

const isStringValue = (v) => slintValueType(v) === 'String';

Try / catch

try {
  const s = toValue(value);
} catch (e) {
  if (e.code === napi.Status.StringExpected) {
    // handle non-string value
  } else throw e;
}

Prevention

When it happens

Trigger: Calling to_value on a Slint Value holding a number, bool, brush, struct, model, or image when a string is expected; reading a numeric .slint property through a string-expecting code path.

Common situations: A .slint property changed from string to another type while JS still treats it as text; locale/translation values stored as non-string types; passing model data of the wrong element type to JS.

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/e8a9617e875fde21. Report an issue: GitHub.

Appendix: source

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

    }
}

/// 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.
fn expect_bool(unknown: Unknown<'_>) -> Result<bool> {
    match unknown.get_type()? {
        ValueType::Boolean => Ok(unknown.coerce_to_bool()?),
        vt => Err(napi::Error::new(
            napi::Status::BooleanExpected,
            format!("expect Boolean, got: {vt:?}"),
        )),
    }
}

#[napi(js_name = "ValueType")]

View on GitHub (pinned to bb937076de)