I have an interface called Converter
export interface Converter<T> {
uuid: CUUID;
decode: (value: Buffer) => Promise<T>;
encode?: (value: T) => Promise<Buffer>;
}
And I am using an array of that in another file:
import{ Converter } from "./characteristic";
export type Converters = Converter<any>[];
export default class Service<C extends Converters> {
private converters: C;
constructor(converters: C = []) {
// ^^^^^^^^^^^^^^^^^^ error
this.converters = converters;
}
}
I am getting an error when I want to assign an empty array as an initial value to that property:
Type 'never[]' is not assignable to type 'C'. 'never[]' is assignable to the constraint of type 'C', but 'C' could be instantiated with a different subtype of constraint 'Converter<any>[]'.ts(2322)
I have tried changing my type to
export type Converters = Converter<any>[] | [];
without any luck
I can do this:
export default class Service<C extends Converters> {
private converters: C | [] = [];
}
but I am not sure why I have to or if that even is an appropriate solution and not just a anti-pattern workaround. I don't even understand what the problem is here.
3.5.3[Converter<string>](a one-tuple type) forC, and you can't assign the value[]to a one-tuple.