oxc-project/oxc · warning
encountered allocation error
Error message
encountered allocation error
What it means
Diagnostic from oxlint's vue/require-typed-ref rule (crates/oxc_linter/src/rules/vue/require_typed_ref.rs), which runs only on TypeScript sources. `ref()` or `shallowRef()` called with no type parameter and no meaningful initial value produces `Ref<any>`, bypassing `noImplicitAny`. The rule reports such calls; a type annotation on the declaring variable also satisfies it.
Source
Thrown at crates/oxc_allocator/src/vec2/raw_vec.rs:871
// ensure that the code generation related to these panics is minimal as there's
// only one location which panics rather than a bunch throughout the module.
#[cold]
#[inline(never)]
fn capacity_overflow() -> ! {
panic!("capacity overflow")
}
/// Handle collection allocation errors
///
// Causing a collection alloc error is rare case, so marked as `#[cold]` and `#[inline(never)]`
// to make the call site function as small as possible, so it can be inlined.
#[inline(never)]
#[cold]
fn handle_error(error: AllocError) -> ! {
match error {
AllocError::CapacityOverflow => capacity_overflow(),
// TODO: call `handle_alloc_error` instead of `panic!` once the AllocErr stored a Layout,
AllocError::AllocErr => panic!("encountered allocation error"),
}
}
#[cfg(test)]
mod tests {
use crate::arena::Arena;
use super::*;
#[test]
fn reserve_does_not_overallocate() {
let arena = Arena::new();
{
let mut v: RawVec<u32, _> = RawVec::new_in(&arena);
// First `reserve` allocates like `reserve_exact`
v.reserve(0, 9);
assert_eq!(9, v.capacity_u32());
}View on GitHub (pinned to e1e7af627c)
Solutions
- Pass an initial value: `const count = ref(0)`.
- Or add a type parameter: `const user = ref<User | null>(null)`.
- Or annotate the variable: `const user: Ref<User | null> = ref(null);`.
- For deliberately untyped refs, disable the rule inline with a comment.
Example fix
// before const user = ref(); const total = shallowRef(null); // after const user = ref<User | null>(null); const total = shallowRef(0);
Defensive patterns
Strategy: type-guard
Validate before calling
// create refs through a helper that refuses bare calls
import { ref, type Ref } from 'vue';
const typedRef = <T,>(): Ref<T> => ref<T>();
// const bad = ref(); // replace with typedRef<User | null>() Type guard
// compile-time guard: `any` satisfies 0 extends 1 & T, so Ref<any> maps to never
import type { Ref } from 'vue';
type NoAny<T> = 0 extends 1 & T ? never : T;
function assertTyped<T>(r: Ref<NoAny<T>>): Ref<T> {
return r as Ref<T>;
}
// const bad = assertTyped(ref()); // compile error
// const ok = assertTyped(ref<number>()); // fine Prevention
- Never write bare ref()/shallowRef() in TS; always ref(value) or ref<Type>().
- Keep noImplicitAny on so downstream use of Ref<any> also surfaces.
- Enable vue/require-typed-ref for .ts and lang=ts files in the oxlint config.
When it happens
Trigger: In TS files (or `lang="ts"` script blocks): `const count = ref();`, `const n = shallowRef();`, or a first argument of `null`/`undefined` (`ref(null)`, `shallowRef(undefined)`) with no type arguments. Passing any real initial value, adding a type parameter (`ref<number>()`), or annotating the variable (`const x: Ref<number> = ...`) avoids the report.
Common situations: Optional state initialized to null and filled later; strict-mode TS codebases plugging `Ref<any>` leaks; refs that hold API data typed only at assignment sites.
Related errors
- Prefer explicitly define the object shape
- Don't use `Function` as a type
- 'The `Object` type actually means "any non-nullish value"
- Do not use any type assertions.
- Always prefer `const x: T = { ... }`.
AI-assisted analysis of oxc-project/oxc@e1e7af627c (2026-08-20).
Data as JSON: /api/errors/640f29d18f3c6765.
Report an issue: GitHub.