Skip to content

allow exporting types using keyof typeof #936

@theoephraim

Description

@theoephraim

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:

image

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    questionFurther information is requested

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions