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

readonly-array ignore return type. #82

Closed
RebeccaStevens opened this issue May 15, 2018 · 1 comment
Closed

readonly-array ignore return type. #82

RebeccaStevens opened this issue May 15, 2018 · 1 comment

Comments

@RebeccaStevens
Copy link
Collaborator

Functions that take an Array cannot be passed a ReadonlyArray. This can somethings make it difficult to work with 3rd party libraries.

Say I'm using a 3rd party library that has a function like so:

function some3rdPartyLibrary(data: number[][]): any {
  // Do stuff.
  return someThing;
}

I cannot call this function with a ReadonlyArray so the following code would fail:

function transposeMatrix<T>(input: ReadonlyArray<ReadonlyArray<T>>): ReadonlyArray<ReadonlyArray<T>> {
  return input[0].map((_, index) => input.map(row => row[index]));
}

function processedData(data: ReadonlyArray<ReadonlyArray<number>>): any {
  return some3rdPartyLibrary(transposeMatrix(data));
}

// [ts] Argument of type 'ReadonlyArray<ReadonlyArray<number>>' is not assignable to parameter of type 'number[][]'.
// Property 'push' is missing in type 'ReadonlyArray<ReadonlyArray<number>>'.

Changing the return type of my transposeMatrix function to T[][] would fix this error but cause a tslint issue with the rule readonly-array.

I'd like to suggest adding an ignore-return-type option to the readonly-array rule.

I feel that once a function has finished and returned a value, in most cases, that function should not force the user to keep that data immutable - that should be up to the user.

The proposal outlined in #74 might be a better way to go however.

@jonaskello
Copy link
Owner

I think the ignore-return-type option would make sense as proposed here. One use case is the one above where you want to produce an Array for input to a third party library. Another as mentioned in #92 (the referenced comment above) would be if you are writing a library and want to return Array. Perhaps for some libraries it would make sense to use ArrayLike or ReadonlyArray as inputs and Array as output.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants