I have:
export const SupportedPrograms = [
'program1',
'program2',
...
] as const;
export type ProgramType = typeof SupportedPrograms[number];
And I have:
const actionType: string = 'p1'
const REDIRECT_MAP = {
p1: 'program1',
p2: 'program2'
};
const redirectType: ProgramType = REDIRECT_MAP[
actionType
] as unknown as ProgramType;
So I want to convert the actionType
, which is a string, to be redirectType
, which needs to be of ProgramType
.
But this Is giving me an error:
Element implicitly has an 'any' type because expression of type 'string' can't be used to index type '{ p1: string; p2: string; }'.
I thought the unknown
would allow it, but no dice. Any tips would be appreciated.
Thanks!
CodePudding user response:
If you only need ProgramType in the end, you can use something like this:
type RedirectMap = { [key: string]: ProgramType };
const REDIRECT_MAP: RedirectMap = {
p1: 'program1',
p2: 'program2'
};