Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

296
Vistas
Typescript: Property 'set' does not exist on type '{}'.ts

This is my store.tsx:

let store = {};

const globalStore = {};

globalStore.set = (key: string, value: string) => {
    store = { ...store, [key]: value };
}

globalStore.get = (key) => {
    return store[key];
}

export default globalStore;

And I use it:

import globalStore from './library/store';

function MyApp({ Component, pageProps }: AppProps) {
    globalStore.set('cookie', pageProps.cookies);
    return <Component {...pageProps} />
}

But I get this error:

Property 'set' does not exist on type '{}'.ts

My code work fine, but I don't want get any error.

Actually, I want to store my data to variable and use it from another component.

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

Well you basically declared the type of the variable as {}, and you can't easily change that after the fact.

So you need to declare the functions inside to object so typescript can infer the correct type of GlobalStore (with the methods):

const store: Record<string, string> = {};

const GlobalStore = {
    set: (key: string, value: string) => {
        store[key] = value;
    },
    get: (key: string): string => {
        return store[key];
    }
}

export default GlobalStore;

The correct type of GlobalStore would be (in opposition to the {} you currently have):

interface GloabStore {
    set(key: string, value: string): void;
    get(key: string): string;
}

And btw, what you are implementing is basically a (Hash)Map, so I think you could just do:

const GlobalStore = new Map<string, string>();

export default GlobalStore;
about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda