Selector does not have a generic `set` variant
See original GitHub issueIn the following pattern, there is a selector
which serves as the public interface to an underlying private atom
. In this situation, it is desirable for the setter to have the ability to accept parameters of a different type than the base value of the selector.
I would like to propose adding a second type parameter to selector
, which serves as the type of the accepted parameter to the setter. This would have a default value of selector<T, TSetter = T>
. This addition of TSetter
will need to propagate up throughout Recoil to ensure all usages remain properly typed.
I will submit a quick barebones PR to demonstrate the gist of it. If this is a pattern you would like to support I can try to introduce it fully and add to the flow typings as well. Thanks!
No functionality change, only adding typings. Contrived example.
class UIStore {
private _appName = "Hello, World!";
private _title = atom<string | null>({
key: "UIStore__Title",
default: null,
});
title = selector<string, string | null>({
key: "UIStore_Title",
get: ({ get }) => {
const title = get(this._title);
if (title === null) return this._appName;
else return `${title} | ${this._appName}`;
},
set: ({ set, get }, newValue) => {
let title = newValue;
if (title === "") title = null;
set(this._title, title);
},
});
}
Issue Analytics
- State:
- Created 3 years ago
- Comments:6 (2 by maintainers)
Top GitHub Comments
Here’s an example of something else that doesn’t work, which I think maybe you were saying above:
I released a
recoil-types
package as a temporary replacement.Add the following path to the
tsconfig.json
file.After the official fix, remove the path and uninstall package
recoil-types
.