-
-
Notifications
You must be signed in to change notification settings - Fork 484
Description
In the past I have found it very useful to be able to export types using keyof typeof someObject
. It had been working previously, but volar has just started complaining:
While in theory exporting types only at the top of the file makes sense, it means that you cannot create a type based on a const. Also, doing so works fine in a regular *.ts
file.
Why would I want to do this? It allows you to have some arbitrarily complex configuration associated with some set of string keys, and then create a type that is only valid when using one of those keys, without having to define the list of possible values more than once.
For example, I can use it to create a list of possible icon names, with a mapping to specific Icons.
import CheckIcon from '@/images/icons/CheckIcon.svg';
import CheckCircleIcon from '@/images/icons/CheckCircle.svg';
// ...
const POSSIBLE_ICONS = {
check: CheckIcon,
'check-circle': CheckCircleIcon,
// ...
};
export type IconNames = keyof typeof POSSIBLE_ICONS;
there are many other cases where this can be useful as well...
Note - may be somewhat related to #923 which is just complaining about exporting additional types altogether.