Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

199
Visualizações
In flow, can you define an exact object type, using string types to define the keys

In flow, is it possible to define an exact object type, where the keys are also types?

In typescript you can accomplish this using strings, like...

export const userSettingsId = 'user-settings';

export type UserSettings = {
    favourites: boolean,
};

export const groupSettingsId = 'group-settings';

export type GroupSettings = {
    enabled: boolean,
};


export type Settings = {
    [userSettingsId]: UserSettings,
    [groupSettingsId]: GroupSettings,
};

const valid: Settings = {
    'user-settings': { favourites: true },
    'group-settings': { enabled: true }
}

const invalid1: Settings = {
    'user-settings': { favourites: true },
    'group-settings': { enabled: true },
    'friend-settings': { enabled: true }
}

const invalid2: Settings = {
    'user-settings': { favourites: true },
    'group-settings': { enabled: true, burritos: true },
}

You cannot use strings as types in flow, but you also do not seem to be able to use string literal types. Flow seems to expect types for keys to be an indexer which doesn't then support this.

This does not work for example...

export type UserSettingsId = 'user-settings';
export const userSettingsId: UserSettingsId = 'user-settings';

export type UserSettings = {|
    favourites: boolean,
|};

export type GroupSettingsId = 'group-settings';
export const groupSettingsId: GroupSettingsId = 'group-settings';

export type GroupSettings = {|
    enabled: boolean,
|};


export type Settings = {|
    [UserSettingsId]: UserSettings,
    [GroupSettingsId]: GroupSettings,
|};

The reason I want to do this, is so I can use the key on its own and ensure it is in sync with the object for when you lookup the key. Does anyone know if there is any solution to this in flow?

about 4 years ago · Juan Pablo Isaza
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda