{"record":{"id":"89358d0c3712dd8f","repo":"slint-ui/slint","slug":"expect-number-got-vt","errorCode":null,"errorMessage":"expect Number, got: {vt:?}","messagePattern":"expect Number, got: (.+?)","errorType":"validation","errorClass":"napi::Error","httpStatus":null,"severity":"error","filePath":"api/node/rust/interpreter/value.rs","lineNumber":33,"sourceCode":"use slint_interpreter::Value;\nuse smol_str::SmolStr;\n\n/// A dynamic-length argument list for calling JS functions with a variable\n/// number of arguments. Implements `JsValuesTupleIntoVec` so it can be used\n/// directly with `Function::call`.\npub struct DynArgs(pub Vec<napi::sys::napi_value>);\n\nimpl JsValuesTupleIntoVec for DynArgs {\n    fn into_vec(self, _env: napi::sys::napi_env) -> Result<Vec<napi::sys::napi_value>> {\n        Ok(self.0)\n    }\n}\n\n/// Safely extract a f64 from an Unknown, failing if the type is wrong.\nfn expect_number(unknown: Unknown<'_>) -> Result<f64> {\n    match unknown.get_type()? {\n        ValueType::Number => unknown.coerce_to_number()?.get_double(),\n        vt => Err(napi::Error::new(\n            napi::Status::NumberExpected,\n            format!(\"expect Number, got: {vt:?}\"),\n        )),\n    }\n}\n\n/// Safely extract a String from an Unknown, failing if the type is wrong.\nfn expect_string(unknown: Unknown<'_>) -> Result<String> {\n    match unknown.get_type()? {\n        ValueType::String => Ok(unknown.coerce_to_string()?.into_utf8()?.as_str()?.to_owned()),\n        vt => Err(napi::Error::new(\n            napi::Status::StringExpected,\n            format!(\"expect String, got: {vt:?}\"),\n        )),\n    }\n}\n\n/// Safely extract a bool from an Unknown, failing if the type is wrong.","sourceCodeStart":15,"sourceCodeEnd":51,"githubUrl":"https://github.com/slint-ui/slint/blob/bb937076de3f7919766c1f25e2e969367cf77e9a/api/node/rust/interpreter/value.rs#L15-L51","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","solutions":["Check the value's type before conversion (inspect the .slint property declaration or the returned ValueType) and only call to_value on numeric values.","Fix the .slint declaration so the property is an `int`/`float` if a number was intended.","In JS, handle the thrown NumberExpected error and fall back to string/boolean conversion paths as appropriate."],"exampleFix":"// before\nconst n = slintValueToJs(anyValue); // throws if not number\n// after\nif (typeof raw === 'number' || isNumericSlintType(raw)) {\n  const n = slintValueToJs(raw);\n}","handlingStrategy":"type-guard","validationCode":"function isSlintNumber(v) {\n  return v != null && (typeof v === 'number' || slintValueType(v) === 'number');\n}","typeGuard":"const isNumberValue = (v) => slintValueType(v) === 'Number';","tryCatchPattern":"try {\n  const n = toValue(value);\n} catch (e) {\n  if (e.code === napi.Status.NumberExpected) {\n    // handle non-numeric value\n  } else throw e;\n}","preventionTips":["Keep .slint property types and JS expectations in sync.","Check the declared property type in the .slint file before reading it from JS.","Add explicit type assertions at the JS/Slint boundary in tests."],"tags":["nodejs","type-mismatch","interpreter","napi"],"backgroundTag":"type-mismatch","analyzedSha":"bb937076de3f7919766c1f25e2e969367cf77e9a","analyzedAt":"2026-09-16T01:37:20.251Z","contentChangedAt":"2026-09-16T01:37:20.251Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}