Code speaks better than language, so:
['a', 'b', 'c'].reduce((accumulator, value) => accumulator.concat(value), []);
The code is very silly and returns a copied Array...
TS complains on concat's argument: TS2345: Argument of type 'string' is not assignable to parameter of type 'ConcatArray'.
The error "Type is not assignable to type 'never'" occurs when we declare an empty array without explicitly typing it and attempt to mutate the array. To solve the error, explicitly type the empty array, e.g. const arr: string[] = []; .
This type represents an array that will never contain any elements (will always be empty). To solve the error, explicitly type the empty array.
I believe this is because the type for []
is inferred to be never[]
, which is the type for an array that MUST be empty. You can use a type cast to address this:
['a', 'b', 'c'].reduce((accumulator, value) => accumulator.concat(value), [] as string[]);
Normally this wouldn't be much of a problem since TypeScript does a decent job at figuring out a better type to assign to an empty array based on what you do with it. However, since your example is 'silly' as you put it, TypeScript isn't able to make any inferences and leaves the type as never[]
.
A better solution which avoids a type assertion (aka type cast) in two variants:
string[]
as the generic type parameter of the reduce
method (thanks @depoulo for mentioning it):['a', 'b', 'c'].reduce<string[]>((accumulator, value) => accumulator.concat(value), []);
accumulator
value as string[]
(and avoid a type cast on []
):['a', 'b', 'c'].reduce((accumulator: string[], value) => accumulator.concat(value), []);
Play with this solution in the typescript playground.
Notes:
Type assertions (sometimes called type casts) should be avoided if you can because you're taking one type and transpose it onto something else. This can cause side-effects since you're manually taking control of coercing a variable into another type.
This typescript error only occurs if the strictNullChecks
option is set to true
. The Typescript error disappears when disabling that option, but that is probably not what you want.
I reference the entire error message I get with Typescript 3.9.2
here so that Google finds this thread for people who are searching for answers (because Typescript error messages sometimes change from version to version):
No overload matches this call. Overload 1 of 2, '(...items: ConcatArray<never>[]): never[]', gave the following error. Argument of type 'string' is not assignable to parameter of type 'ConcatArray<never>'. Overload 2 of 2, '(...items: ConcatArray<never>[]): never[]', gave the following error. Argument of type 'string' is not assignable to parameter of type 'ConcatArray<never>'.(2769)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With