Created
May 17, 2025 04:58
-
-
Save VictorQueiroz/d36149a841d604fb9ec73dfb3279185c to your computer and use it in GitHub Desktop.
`as` as a no-no
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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