[英]TS error: Argument of type 'null' is not assignable to parameter of type 'Error | PromiseLike<Error | undefined> | undefined'.ts(2345)
I am using Mobx Persist Store that saves MobX Store to Local Storage.我正在使用将MobX存储保存到本地存储的 Mobx 持久存储。
The docs doesn't have a TS version so I modified 2 lines (one in readStore
& another in writeStore
which you can compare with https://github.com/quarrant/mobx-persist-store#with-mobx-6 ) to solve TS errors but that caused another error:文档没有 TS 版本,所以我修改了 2 行(一个在readStore
中,另一个在writeStore
中,您可以与https://github.com/quarrant/mobx-persist-store#with-mobx-6进行比较)到解决 TS 错误但导致另一个错误:
import {
persistence,
useClear,
useDisposers,
isSynchronized,
StorageAdapter,
} from 'mobx-persist-store'
import { FrameItStore } from '@/store/index'
function readStore(name: string) {
return new Promise<string>((resolve) => {
const data = localStorage.getItem(name) || '{}'
resolve(JSON.parse(data))
})
}
function writeStore(name: string, content: string) {
return new Promise<Error | undefined>((resolve) => {
localStorage.setItem(name, JSON.stringify(content))
resolve(null)
})
}
export default persistence({
name: 'FrameItStore',
properties: ['counter'],
adapter: new StorageAdapter({
read: readStore,
write: writeStore,
}),
reactionOptions: {
// optional
delay: 2000,
},
})(new FrameItStore())
I am getting an error on the null
in resolve(null)
inside of writeStore
.我在null
内部的resolve(null)
中的writeStore
上遇到错误。
Here's the error:这是错误:
Argument of type 'null' is not assignable to parameter of type 'Error | 'null' 类型的参数不能分配给'Error | 类型的参数PromiseLike<Error | PromiseLike<错误 | undefined> |未定义> | undefined'.ts(2345)未定义'.ts(2345)
How can I solve it?我该如何解决?
unknown
is not compatible with string
or undefined
. unknown
与string
或undefined
不兼容。 I believe that JSON.parse is returning unknown
.我相信 JSON.parse 正在返回unknown
。 So rather than just resolving JSON.parse(data)
you have to determine its type.因此,您必须确定其类型,而不仅仅是解析JSON.parse(data)
。 You can do this by using conditional checks.您可以通过使用条件检查来做到这一点。 Once you are sure it's a string
you can resolve it, otherwise resolve undefined
.一旦你确定它是一个string
,你就可以解析它,否则解析undefined
。
function readStore(name: string) {
return new Promise((resolve) => {
const data = localStorage.getItem(name) || '{}'
const output = JSON.parse(data);
if (typeof output === "string") resolve(output);
resolve(undefined);
})
}
I think I solved it as I see no errors now.我想我解决了它,因为我现在没有看到任何错误。 I had to remove JSON.parse
from readStore
as well as use resolve(undefined)
instead of resolve(null)
in writeStore
like:我必须从readStore
中删除JSON.parse
并在writeStore
中使用resolve(undefined)
而不是resolve(null)
,例如:
import {
persistence,
useClear,
useDisposers,
isSynchronized,
StorageAdapter,
} from 'mobx-persist-store'
import { FrameItStore } from '@/store/index'
function readStore(name: string) {
return new Promise<string>((resolve) => {
const data = localStorage.getItem(name) || '{}'
resolve(data)
})
}
function writeStore(name: string, content: string) {
return new Promise<Error | undefined>((resolve) => {
localStorage.setItem(name, JSON.stringify(content))
resolve(undefined)
})
}
export const PersistState = persistence({
name: 'FrameItStore',
properties: ['counter'],
adapter: new StorageAdapter({
read: readStore,
write: writeStore,
}),
reactionOptions: {
// optional
delay: 2000,
},
})(new FrameItStore())
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.