0
I’m trying to export an object with my valid types and create a type based on that. So far so good, the problem is the interface that expects a string but is not accepting the object prop (which is a string) as valid.
export const SortValues = {
NEWER: 'NEWER',
OLDER: 'OLDER',
NAME_ASC: 'NAME_ASC',
NAME_DESC: 'NAME_DESC',
}
interface Option<T = string> {
key: T;
value: string;
}
type SortValuesType = keyof typeof SortValues;
// type SortValuesType = 'NEWER' | 'OLDER' | 'NAME_ASC' | 'NAME_DESC'; // Não funciona também.
export const options: Array<Option<SortValuesType>> = [
{ key: SortValues.NEWER, value: 'Mais novos' },
{ key: SortValues.OLDER, value: 'Mais antigos' },
{ key: SortValues.NAME_ASC, value: 'A - Z' },
{ key: SortValues.NAME_DESC, value: 'Z - A' },
];
I’m getting the bug "Type 'string' is not assignable to type '"NEWER" | "OLDER" | "NAME_ASC" | "NAME_DESC"'."
in the key property of options.
It works if I declare Sortvalues as an Enum, since it is causing me problems elsewhere, so I really wanted to export the valid options as an object.
Here’s a snippet with the code in question. https://codesandbox.io/s/fqide
Welcome to Stack Overflow in English. Please click edit and translate the question.
– Luiz Augusto
@Luizaugusto Feito, I vacillated at the time of posting. I didn’t notice that I was in the en. Mal aê.
– Renato Rodrigues