|
|
|
|
|
by eyelidlessness
999 days ago
|
|
> for example, TypeScript thinks `$state` and `$derived` are just the identity function That seems like a missed opportunity on Svelte’s part… but hard to fault, because TypeScript doesn’t support nominal primitive types very well. Ideally it would be something like Reactive<T> to signal (ha) that it’s not just a plain value. |
|
type Reactive<T> = T; function $state<T>(value: T): Reactive<T>
...then TypeScript will 'unwrap' the type anyway, unless you do funky stuff like this...
type Reactive<T> = T & { [uniquesymbol]: any };
...in which case things like `value += 1` will cause type errors because it coercies `value` from `Reactive<number>` to `number`.
But it also creates problems here:
let message = $state('hello'); obj = { message };
The type of `obj.message` is Reactive<string>, but it's _not_ reactive — it's a non-reactive snapshot of the value when the object was created.
It's possible that we can do some fun stuff with TypeScript plugins, but we haven't dived too deeply into it yet.