简体   繁体   English

Typescript:类型“{}”.ts 上不存在属性“set”

[英]Typescript: Property 'set' does not exist on type '{}'.ts

This is my store.tsx :这是我的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类型“{}”.ts 上不存在属性“set”

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.实际上,我想将我的数据存储到变量中并从另一个组件中使用它。

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):所以你需要将里面的函数声明为 object 以便 typescript 可以推断出正确的GlobalStore类型(使用方法):

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): GlobalStore的正确类型是(与您当前拥有的{}相反):

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:顺便说一句,您正在实施的基本上是(哈希) Map ,所以我认为您可以这样做:

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

export default GlobalStore;

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在Typescript中为其子项设置功能道具类型? TS2339类型上不存在“孩子”属性 - How to set function props type for its child in Typescript? Property 'children' does not exist on type TS2339 Typescript 通用:“T”类型上不存在属性“pass”.ts(2339) - Typescript generic : Property 'pass' does not exist on type 'T'.ts(2339) Typescript 错误:属性“status”在类型“never”上不存在。ts(2339) - Typescript error: Property 'status' does not exist on type 'never'.ts(2339) 类型'A'.ts(2339)上不存在属性'ref' - React,TypeScript - Property 'ref' does not exist on type 'A'.ts(2339) - React, TypeScript typescript 错误与属性“计数器”不存在类型“{}”.ts - typescript error with Property 'counter' does not exist on type '{}'.ts "<i>TypeScript: Property &#39;data&#39; does not exist on type &#39;{ children?: ReactNode;<\/i> TypeScript:类型&#39;{ children?:ReactNode;上不存在属性&#39;data&#39;<\/b> <i>}&#39;.<\/i> }&#39;。<\/b> <i>ts(2339)<\/i> ts(2339)<\/b>" - TypeScript: Property 'data' does not exist on type '{ children?: ReactNode; }'. ts(2339) 类型{}不存在Typescript属性 - Typescript property does not exist on type {} 类型 [] 打字稿上不存在属性 - property does not exist on type [] typescript 属性在类型上不存在-TypeScript - Property does not exist on type - TypeScript (TS)属性行在类型HTMLInputElemet上不存在 - (TS)Property Rows does not exist on type HTMLInputElemet
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM