Skip to content

Instantly share code, notes, and snippets.

@VictorQueiroz
Created May 17, 2025 04:58
Show Gist options
  • Select an option

  • Save VictorQueiroz/d36149a841d604fb9ec73dfb3279185c to your computer and use it in GitHub Desktop.

Select an option

Save VictorQueiroz/d36149a841d604fb9ec73dfb3279185c to your computer and use it in GitHub Desktop.
`as` as a no-no
interface IMyWriteableSignal<T> {
(): T;
set: (value: T) => void;
update: (updater: (currentValue: T) => T) => void;
}
function myWriteableSignal<T>(initialValue: T): IMyWriteableSignal<T> {
let value = initialValue;
function signal() {
return value;
}
signal.set = (newValue: T) => {
value = newValue;
};
signal.update = (updater: (v: T) => T) => {
value = updater(value);
};
return signal;
}
const counter = myWriteableSignal(0);
counter.set(10);
counter.update(n => n + 2);
console.log(counter()); // 12
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment