Skip to content

Autocomplete for String Union Types #9512

Closed
@Meligy

Description

@Meligy

Let's say I have the following type:

 type Cars = 'Ferrari' | 'Volvo' | 'Porsche';

It will be awesome if I can treat it like an enum, meaning I can write Cars. and get intellisense for Ferari, Volve, and Porsche.

Today, you do not get that.

You can have funky workarounds to make the number based enum become a string

Like this (runnable sample):

enum Cars {
    Ferrari = <any> "Ferrari",
    Volvo = <any> "Volvo",
    Porsche = <any> "Porsche"
}

But that's hacking, even though it opens the question why not make this a core feature.

Or, you can use namespaces, like (runnable sample):

type Cars = 'Ferrari' | 'Volvo' | 'Porsche';
namespace Cars {
    export const Ferrari : Cars = "Ferrari";
    export const Volvo : Cars = "Volvo";
    export const Porsche : Cars = "Porsche";
}

But obviously, this way you can get into errors, with both the type and namespace potentially going out of sync due to human error.

Ideally, the very first line should in itself allow you to have autocomplete, similar to what enum does. Or alternatively of course support string enum types.

Metadata

Metadata

Assignees

No one assigned

    Labels

    DuplicateAn existing issue was already created

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions