简体   繁体   English

TS 错误:“null”类型的参数不可分配给“错误”类型的参数 | 承诺喜欢<error | undefined> | 未定义'.ts(2345)</error>

[英]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 . unknownstringundefined不兼容。 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.

相关问题 人员类型的 TypeScript 错误 (TS2345) 参数 | undefined 不能分配给 Person 类型的参数 - TypeScript Error (TS2345) argument of type Person | undefined is not assignable to paramater of type Person TS2345:“日期”类型的参数 | null' 不可分配给类型为“string |”的参数日期 | 不明确的' - TS2345: Argument of type 'Date | null' is not assignable to parameter of type 'string | Date | undefined' Angular2 / TypeScript:错误TS2345:类型'String'的参数不能分配给'string'类型的参数 - Angular2/TypeScript: error TS2345: Argument of type 'String' is not assignable to parameter of type 'string' Firestore != 查询错误:“!=”类型的参数不可分配给“WhereFilterOp”.ts(2345) 类型的参数 - Firestore != query error: Argument of type '"!="' is not assignable to parameter of type 'WhereFilterOp'.ts(2345) Ngrx/effects:获取错误 TS2345:“Product[]”类型的参数不可分配给“Product”类型的参数 - Ngrx/effects: get error TS2345: Argument of type 'Product[]' is not assignable to parameter of type 'Product' 错误 TS2345:“事件”类型的参数不可分配给“{目标:{值:字符串; }; }' - error TS2345: Argument of type 'Event' is not assignable to parameter of type '{ target: { value: string; }; }' 错误TS2345:类型为&#39;(a:Test,b:Test)=&gt;布尔| 1&#39;不能分配给类型&#39;(a:Test,b:Test)=&gt; number&#39;的参数 - error TS2345: Argument of type '(a: Test, b: Test) => boolean | 1' is not assignable to parameter of type '(a: Test, b: Test) => number' TS2345:“事件”类型的参数不可分配给“HtmlInputEvent”类型的参数 - TS2345: Argument of type 'Event' is not assignable to parameter of type 'HtmlInputEvent' typescript:类型&#39;any []&#39;的参数不能分配给&#39;[]&#39;类型的参数.ts(2345) - typescript : Argument of type 'any[]' is not assignable to parameter of type '[]'.ts(2345) 'string | 类型的参数 undefined' 不可分配给“string”类型的参数 - TypeScript 错误 - Argument of type 'string | undefined' is not assignable to parameter of type 'string' - TypeScript error
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM