Omnistac / zedux

:zap: A Molecular State Engine for React
https://Omnistac.github.io/zedux/
MIT License
376 stars 7 forks source link

feat(atoms): give atom instances access to their real template type #81

Closed bowheart closed 1 year ago

bowheart commented 1 year ago

Description

For TypeScript users, an atom instance's template type (e.g. when using instance.template) is inferred to be an instance of the Zedux built-in AtomTemplate class. But if the instance was created from a custom class that extends AtomTemplate, the type information of that child class would be lost. For example:

class MyTemplate extends AtomTemplate {
  public customMethod() {
    // ... implemention ...
  }
}

const myCustomTemplateAtom = new MyTemplate('myCustomTemplate', () => 'example state')

const instance = ecosystem.getInstance(myCustomTemplateAtom)
instance.template.customMethod() // actually works! But TS used to complain

Now TS doesn't complain; the type information of the instance's actual atom template is retained