A simple TypeScript state utility using classes.
It allows you to create a state container with getter and setter methods, fully typed and easy to use.
- Fully typed with TypeScript generics
- Minimal, no dependencies
- Encapsulates state in a class instance
- Simple API:
.value()to read,.setValue()to update - Works for any type: boolean, string, number, objects, etc.
Copy index.ts into your project:
import { makeState } from "./makest"const toggle = makeState<boolean>(false)
// Get the current value
console.log(toggle.value()) // false
// Update state
toggle.setValue(true)
console.log(toggle.value()) // trueconst word = makeState<string>("Hello")
console.log(word.value()) // "Hello"
// Update value
word.setValue("This is the new word")
console.log(word.value()) // "This is the new word"const counter = makeState<number>(0)
// Read current value
console.log(counter.value()) // 0
// Increment manually
counter.setValue(counter.value() + 1)
console.log(counter.value()) // 1
// Increment using setTimeout
setTimeout(() => counter.setValue(counter.value() + 5), 2000)
setTimeout(() => console.log(counter.value()), 4000) // 6| Method | Description |
|---|---|
value() |
Returns the current state value. |
setValue(v) |
Updates the state to a new value v. |
- Each call to
makeStatecreates a separate state instance. - Use it for toggles, counters, text, or any simple state.
- No hooks, subscriptions, or React dependencies — just clean TypeScript.
See examples.ts for full working examples.
- Encapsulates state in a single object
- Provides getter and setter functions for consistent updates
- Can easily be expanded later (e.g., add listeners or logging)
- Type-safe for any data type