Typing state variables

let status: 'OPEN' | 'CLSOED' = $state('OPEN')

state in functions

The state rune can be used anywhere - even in functions or classes.

<script>
  
  function counter() {
    let count = $state(0) 
 
    function getCount() {
      return count
    }
    
    function increment() {
      count = (count + 1)
    }
 
    function decrement() {
      count = (count - 1)
    }
 
    return {
    getCount,
    increment,
    decrement,
  };
  }
 
  const counterInstance = counter()
</script>
 
<div>
  <h1>Counter: {counterInstance.getCount()}</h1>
  <button onclick={counterInstance.increment}>Increment</button>
  <button onclick={counterInstance.decrement}>Decrement</button>
</div>