I defined an array of an interface
interface Converter<T = Buffer> {
name: string;
uuid: CUUID;
decode: (value: Buffer) => T;
}
type Converters = ReadonlyArray<Converter<any>>;
but for my usecase I need to cast every item in that array using a const assertion:
const infoConverter = {
name: "info",
uuid: "180a",
decode: (v: Buffer) => v.toString()
} as const;
const pressureConverter = {
name: "pressure",
uuid: "1810",
decode: (v: Buffer) => v.readInt32BE(0)
} as const;
const converters = [infoConverter, pressureConverter];
I would not mind doing that, but I am writing a library and users of that library are providing that array. So in order for the library to work properly I will have to ask the users to use a const assertion whenever they work with it, which is not quite optimal.
For me, it would be great if the could just provide the library with their data and I could automatically do a const assertion.
So I have this class
class Service<C extends Converters> {
private converters?: Converters;
constructor(converters?: C) {
this.converters = converters;
}
}
and I wonder if there is a way I could do something like this:
class Service<C extends Converters> {
private converters?: EveryItemIsAConst<Converters>;
constructor(converters?: C) {
this.converters = converters as EveryItemIsAConst<C>;
}
}
and the users could just do
const infoConverter = {
name: "info",
uuid: "180a",
decode: (v: Buffer) => v.toString()
};
const pressureConverter = {
name: "pressure",
uuid: "1810",
decode: (v: Buffer) => v.readInt32BE(0)
};
const converters = [infoConverter, pressureConverter];
const service = new Service(converters)
and the EveryItemIsAConst would make sure as const is applied to every item in the converters array.
new Service(converters)the compiler has already widened the type ofconverters[0].nameandconverters[1].nametostring, and there's no way to get it back. There are different ways to keep string literals narrow but all of them will require users ofServiceto do something. e.g.,const infoConverter = {...} as const, orconst infoConverter = asConverter({})(for an appropriateasConverter()function).