SolidJSS
SolidJS14mo ago
utrain

How to make setStore treat a class/interface as an opaque value instead of a partiable one?

So, I'm currently using a UOM library called @buge/ts-units. It has this interface called Quantity that I want to use in my stores. For example:
type Rectangle = {
  // Length is an instance of Quanity
  // Length = Quantity<...>
  width: Length 
  height: Length
}

The problem is that whenever I want to use
setStore
with the path syntax, this is allowed:
// No errors:
setRectangleStore("width", 0)

// function inches(length: number): Length
// It should only allow:
setRectangleStore("width", inches(0))

It seems to be because in Typescript, the type used for the last argument is Partial<Length>, which allows a number(???) to be accepted. Ignoring that, I would want to treat
Length
as a opaque, unpatchable, immutable value just like any other primitive type (boolean, number, bigint, et cetera). The work around I found is essentially avoiding the path syntax and merge at the top-level:
// Good
setRectangleStore({ width: inches(0) })

// Errors as expected:
setRectangleStore({ width: 0 })
Was this page helpful?