You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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:
functionsome3rdPartyLibrary(data: number[][]): any{// Do stuff.returnsomeThing;}
I cannot call this function with a ReadonlyArray so the following code would fail:
functiontransposeMatrix<T>(input: ReadonlyArray<ReadonlyArray<T>>): ReadonlyArray<ReadonlyArray<T>>{returninput[0].map((_,index)=>input.map(row=>row[index]));}functionprocessedData(data: ReadonlyArray<ReadonlyArray<number>>): any{returnsome3rdPartyLibrary(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.
The text was updated successfully, but these errors were encountered:
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.
Functions that take an
Array
cannot be passed aReadonlyArray
. 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:
I cannot call this function with a
ReadonlyArray
so the following code would fail:Changing the return type of my
transposeMatrix
function toT[][]
would fix this error but cause a tslint issue with the rulereadonly-array
.I'd like to suggest adding an
ignore-return-type
option to thereadonly-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.
The text was updated successfully, but these errors were encountered: