首页 > 解决方案 > 打字稿中的可空字符串:函数将空值作为字符串返回

问题描述

我有一个本地存储的包装器。

getItem(key: string): string | null {

    let value = localStorage.getItem(key);

    if(value){
        return value;
    }

    return null;
}

getDateFormat(): null | string {

    let value = this.getItem('dateFormat');

    if(value){
        return value;
    }

    return null;
}

当我在外部(在反应组件中)调用方法时 getDateformat,我得到了意想不到的结果-

var currentFormat = AppStorage.getDateFormat(); // get "null" instead of null

同时在getItem("non-existing-key")外面返回正常的null,但是......在里面 getDateformat返回“null”。任何想法?

标签: typescript

解决方案


推荐阅读