Skip to content

requiredRef ​

Creates a Vue ref that must be assigned before it can be read. Accessing it too early throws immediately, which makes missing initialization obvious during development instead of failing later with undefined behavior.

The helper returns a normal Ref<T>, so it works anywhere a Vue ref is accepted. The difference is runtime behavior: the first read before assignment throws an error.

Importing ​

ts
import { requiredRef } from '@almighty-shogun/common';

Usage ​

ts
import { requiredRef } from '@almighty-shogun/common';

const user = requiredRef<{ id: string; name: string }>();

user.value = { id: '1', name: 'Ada' };

Returns

A Vue ref that throws when read before assignment.

Type signature ​

ts
declare function requiredRef<T>(): Ref<T>;

All packages are released under the MIT License.