guide/src/reference/attributes/on-js-imports/no_upcast.md
no_upcastThe no_upcast attribute disables automatic generation of Upcast trait implementations for an imported type.
By default, wasm-bindgen automatically generates the following Upcast implementations for imported types:
Upcast<JsValue> - all types can upcast to JsValueUpcast<Self> - identity upcast for non-generic typesType<T> → Type<U> when T: Upcast<U>)Upcast<SuperClass> for each type in the extends attributeUse no_upcast when you need to provide custom Upcast implementations, for example when a type has special covariance rules.
use wasm_bindgen::convert::Upcast;
#[wasm_bindgen]
extern "C" {
// Automatic Upcast implementations are generated
#[wasm_bindgen(extends = Object)]
type NormalType;
// No automatic Upcast implementations - must be provided manually
#[wasm_bindgen(extends = Object, no_upcast)]
type CustomType<T>;
}
// Provide custom Upcast implementations for CustomType
impl<T> Upcast<JsValue> for CustomType<T> {}
impl<T> Upcast<Object> for CustomType<T> {}
// ... additional custom implementations
This is useful for types that have:
Upcast implementations