Skip to content

Dealing with readonly types returned from immutable array operations #565

@synthet1c

Description

@synthet1c

I'm having a problem using rambda for ngrx selectors that requires me to explicitly set readonly on immutable values to match the typescript definitions in rambda. It is also out of alignment with Ramda which does not force immutable return values through readonly.

Would you consider removing the readonly return type to keep type definitions as concise and compatible as possible? or do you think it's important to force immutable Arrays and Objects?

If I use Rambda.filter in my selector the return type is readonly Foo[]

const selectRambdaFoos = createSelector(
  selectAllFoos,
  Rambda.filter<Foo>(identity), // becomes `readonly Foo[]`
)
// => Selector<object, readonly Foo[]>

If I use Ramda.filter in my selector the return type is Foo[] (although I can't even get it to work, but the type signature should do this)

const selectRamdaFoos = createSelector(
  selectAllFoos,
  Ramda.filter<Foo>(identity),
)
// => Selector<object, Foo[]>

Where using imperative code wouldn't require the interface to be readonly

const selectImperativeFoos = createSelector(
  selectAllFoos,
  foos => foos.filter(R.identity),
)
// => Selector<object, Foo[]>

I can get around it by changing the type where I consume the selected observable, but is there any better way to deal with it? I would like to use rambda for my new job, but don't think it's a good idea at the start to change all their code to suit rambda, and I'm not sure what will happen if I then pipe that value into something else that I don't control that won't accept readonly interfaces.

@Component({ ... })
class FooComponent implements OnInit {

  $ramdaFoos: Observable<Foo[]>

  /* readonly property added to interface */
  $rambdaFoos: Observable<readonly Foo[]>

  constructor(private store: Store<AppState>){}

  ngOnInit(): void {
    this.rambdaFoos$ = this.store.pipe(select(selectRambdaFoos))
    this.ramdaFoos$ = this.store.pipe(select(selectRamdaFoos))
  }
}

Here is a working stackblitz if you need it.
https://stackblitz.com/edit/rambda-readonly-issue?file=src/app/courses/reducers/courses.reducer.ts
https://stackblitz.com/edit/rambda-readonly-issue?file=src/app/courses/components/course-list/course-list.component.ts

Cheers

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions