The first param is a list of allowed strings that the user can use. It works fine.
The second param is an array of values, but it must be one of the multiple arrays of constants defined in the type. It works fine until I call methods like includes or indexOf on it. When I do it tells me that field is of type never and I want to understand why and how to make it work properly.
The commented code works fine (kind of a manual job of includes or indexOf), but it's not ideal and makes me even more confused on why the original doesn't work.
My code:
type AvailableConfigFields = ("a" | "b" | "c" | "more fields")[];
type AvailableProviderFields = ["a"] | ["b"] | ["c"];
function filterFieldsForProvider(
requestedFields: AvailableConfigFields,
providerFields: AvailableProviderFields
) {
// When calling `.includes(field)` TS throws the error below:
// Argument of type 'string' is not assignable to parameter of type 'never'.
// Type 'string' is not assignable to type 'never'.
return requestedFields.filter((field) => providerFields.includes(field));
// The code below works fine, but obviously I don't want to do that
// return requestedFields.filter((field) => {
// let isThere = false;
// providerFields.forEach((value) => {
// if (value === field) isThere = true;
// });
// return isThere;
// });
}