Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Selectors: typed projector methods #3571

Closed
2 tasks
timdeschryver opened this issue Sep 12, 2022 · 1 comment · Fixed by #3581 or #3640
Closed
2 tasks

Selectors: typed projector methods #3571

timdeschryver opened this issue Sep 12, 2022 · 1 comment · Fixed by #3581 or #3640
Labels
15.x Accepting PRs Breaking Change community watch Someone from the community is working this issue/PR Project: Store

Comments

@timdeschryver
Copy link
Member

Information

This issue comes from a discussion #3097

Currently when using the selector.projector method, we can pass it any argument(s) and it won't complain. With this change we want to make the projectors type-safe so that mistakes can be caught earlier.

Describe any alternatives/workarounds you're currently using

/

I would be willing to submit a PR to fix this issue

  • Yes
  • No
@david-shortman
Copy link
Contributor

Started a PR, feel free to assign

@timdeschryver timdeschryver added Accepting PRs community watch Someone from the community is working this issue/PR labels Sep 19, 2022
@markostanimirovic markostanimirovic moved this to In Progress in NgRx v15 Oct 20, 2022
Repository owner moved this from In Progress to Done in NgRx v15 Nov 2, 2022
brandonroberts pushed a commit that referenced this issue Nov 2, 2022
BREAKING CHANGE:

The projector function on the selector is type-safe by default.

BEFORE:

The projector is not type-safe by default, allowing for potential mismatch types in the projector function.

```ts
const mySelector = createSelector(
  () => 'one',
  () => 2,
  (one, two) => 3
)

mySelector.projector() // <- type is projector(...args: any[]): number
```

AFTER:

The projector is strict by default, but can be bypassed with an `any` generic parameter.

```ts
const mySelector = createSelector(
  () => 'one',
  () => 2,
  (one, two) => 3
)

mySelector.projector() // <- Results in type error. Type is projector(s1: string, s2: number): number
```

To retain previous behavior

```ts
const mySelector = createSelector(
  () => 'one',
  () => 2,
  (one, two) => 3
)

(mySelector.projector as any)()
```

Closes #3571
brandonroberts pushed a commit that referenced this issue Nov 2, 2022
BREAKING CHANGE:

The projector method has become strict

BEFORE:

You could pass any arguments to the projector method

const selector = createSelector(
  selectString, // returning a string
  selectNumber, // returning a number
  (s, n, prefix: string) => {
    return prefix + s.repeat(n);
  }
)

// you could pass any argument
selector.projector(1, 'a', true);

AFTER:

const selector = createSelector(
  selectString, // returning a string
  selectNumber, // returning a number
  (s, n, prefix: string) => {
    return prefix + s.repeat(n);
  }
)

// this throws
selector.projector(1, 'a', true);
// this does not throw because the arguments have the correct type
selector.projector(1, 'a', 'prefix');

Closes #3571
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
15.x Accepting PRs Breaking Change community watch Someone from the community is working this issue/PR Project: Store
Projects
No open projects
Status: Done
2 participants