Typescript React 在来自 nativeBase 框架的 colorModeMangager 上发生类型错误

问题描述 投票:0回答:1

我在 typescript React 中使用 NativeBase Framework 作为我的 UI,并尝试设置一个颜色模式管理器来全局管理我的应用程序中的暗/亮模式,就像 就像他们在文档中实现的那样:

const colorModeManager: StorageManager = { get: async () => { let val = localStorage.getItem('@color-mode'); return val === 'dark' ? 'dark' : 'light'; }, set: async (value: ColorMode) => { let strValue = value ? value.toString() : ''; localStorage.setItem('@color-mode', strValue); }, };
...

<NativeBaseProvider colorModeManager={colorModeManager}> (...) </NativeBaseProvider>
但是我在 StorageManager 对象中的“get”键上注释了此类型错误:

Type '{ get: () => Promise<"light" | "dark">; set: (value: ColorMode) => Promise<void>; }' is not assignable to type 'StorageManager'. Object literal may only specify known properties, and 'get' does not exist in type 'StorageManager'.ts(2322) (property) get: () => Promise<"dark" | "light">
NativeBaseProvider 的 colorModeManager 属性上出现此错误:

Type 'StorageManager' is missing the following properties from type 'StorageManager': get, setts(2739) NativeBaseProvider.d.ts(7, 5): The expected type comes from property 'colorModeManager' which is declared here on type 'IntrinsicAttributes & NativeBaseProviderProps' (property) NativeBaseProviderProps.colorModeManager?: StorageManager | undefined
我检查了native-base的StorageManager接口:

export interface StorageManager { get(init?: ColorMode): Promise<ColorMode | undefined>; set(value: ColorMode): void; }
我认为它符合我定义的内容(嗯,它应该是,它只是从他们的文档复制粘贴),但我收到类型错误。有人知道我在这里缺少什么吗?

您还可以在我的 Github 上查看此状态下的整个源代码:

https://github.com/Fabian5150/game-collection-app/tree/489c3975b018ac689bcd37970be586afde8815cb

reactjs typescript typeerror react-context native-base
1个回答
0
投票
尝试从“native-base”导入 StorageManager

© www.soinside.com 2019 - 2024. All rights reserved.