question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Selector does not have a generic `set` variant

See original GitHub issue

In 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:closed
  • Created 3 years ago
  • Comments:6 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
adrianbwcommented, Jul 9, 2020

Here’s an example of something else that doesn’t work, which I think maybe you were saying above:

export const exportQueueState = selector<string[]>({
  key: `${namespace}.exportQueue`,
  get: ({ get }): string[] => {
    return get(ExportWatcherStore).exportQueue;
  },
  set: ({ set }, newValue) =>
    set(ExportWatcherStore, (oldState) => ({
      ...oldState,
      exportQueue: oldState.exportQueue.push(newValue), // Argument of type 'string[] | DefaultValue' is not assignable to parameter of type 'string'.
    })),
});
0reactions
sanonzcommented, Apr 12, 2021

I released a recoil-types package as a temporary replacement.

npm i recoil-types

Add the following path to the tsconfig.json file.

{
  "compilerOptions": {
    "paths": {
      "recoil": ["node_modules/recoil-types/index"]
    }
  }
}

After the official fix, remove the path and uninstall package recoil-types.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Is it possible to have a generic element in a CSS selector?
For instance, let's say that I want every element under an h1 of class title to have a margin-top of 30px , because...
Read more >
6 Selectors
Type selectors match all instances of the element type in the document. Element type names are case-insensitive if the document is in HTML,...
Read more >
Unicode variant selector (u+FE00) not working with Noto Sans ...
I have text, in TexPad on macOS, which in the editor view shows the dots (i.e. the variant selector character is working), but...
Read more >
Universal selectors - CSS: Cascading Style Sheets | MDN
The CSS universal selector ( * ) matches elements of any type. ... The universal selector is a special type selector and can...
Read more >
Using dynamic selectors in Optimizely Web
The variation code doesn't know what to change, since the initial selector no longer exists. Often, you can also tell if you have...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Hashnode Post

No results found