With the following code we produce two arrays:
const PROVIDERS: {
PROVIDER_1: 'PROVIDER_1';
PROVIDER_2: 'PROVIDER_2';
PROVIDER_3: 'PROVIDER_3';
} = {
PROVIDER_1: 'PROVIDER_1',
PROVIDER_2: 'PROVIDER_2',
PROVIDER_3: 'PROVIDER_3',
};
const GOOD_PROVIDERS = [PROVIDERS.PROVIDER_1, PROVIDERS.PROVIDER_2];
const BAD_PROVIDERS = [PROVIDERS.PROVIDER_3];
The types are:
const GOOD_PROVIDERS: ("PROVIDER_1" | "PROVIDER_2")[]
const BAD_PROVIDERS: ("PROVIDER_3")[]
Then, if I try to check if a value is included in one of those arrays I face several compilation errors that I don't know how to handle:
const provider: 'PROVIDER_1' | 'PROVIDER_2' | 'PROVIDER_3' = getProvider();
const isGood = GOOD_PROVIDERS.includes(provider);
const isBad = BAD_PROVIDERS.includes(provider);
The includes of GOOD_PROVIDERS throws this error
Argument of type '"PROVIDER_1" | "PROVIDER_2" | "PROVIDER_3"' is not assignable to parameter of type '"PROVIDER_1" | "PROVIDER_2"'. Type '"PROVIDER_3"' is not assignable to type '"PROVIDER_1" | "PROVIDER_2"'.ts(2345)
The includes of BAD_PROVIDERS throws this error
Argument of type '"PROVIDER_1" | "PROVIDER_2" | "PROVIDER_3"' is not assignable to parameter of type '"PROVIDER_3"'. Type '"PROVIDER_1"' is not assignable to type '"PROVIDER_3"'.ts(2345)
Why does Array.includes assume that the argument is already in the list? Is there any other way to check this?