@ludeschersoftware/ref
v1.0.0
Published
A Minimal Mutable Reference Wrapper for JavaScript
Readme
🔗 Ref
A minimal, type-safe mutable reference wrapper for JavaScript and TypeScript.
Ref provides a simple way to encapsulate and mutate values without losing type safety. Inspired by reactive programming patterns, it's perfect for managing shared state, building custom reactive systems, or simplifying value tracking in functional code.
🚀 Features
- ✅ Generic type support (
Ref<T>) - 🔄 Mutable
.valueproperty for easy get/set - 🧠 Great for state containers, observers, and dependency injection
- 🪶 Lightweight and framework-agnostic
📦 Installation
npm install @ludeschersoftware/ref
# or
yarn add @ludeschersoftware/ref🧪 Usage
import { Ref } from '@ludeschersoftware/ref';
const count: Ref<number> = new Ref(0);
// Access the value
console.log(count.value); // 0
// Update the value
count.value += 1;
console.log(count.value); // 1🛠️ API
Ref<T>
A generic class that wraps a value of type T.
Constructor
new Ref<T>(initialValue: T)Properties
value: T– Get or set the current value.
🧩 Use Cases
- Shared mutable state across functions
- Custom reactive systems
- Dependency injection containers
- Functional programming with controlled mutability
📚 Example: Counter
function createCounter(): Ref<number> {
const counter = new Ref(0);
setInterval(() => counter.value++, 1000);
return counter;
}
const myCounter = createCounter();
setTimeout(() => console.log(myCounter.value), 3000); // ~3🧼 License
MIT © Johannes Ludescher
💬 Feedback
Got ideas or improvements? Feel free to open an issue or submit a PR. Contributions are welcome!
