typescript - 打字稿中的可空字符串:函数将空值作为字符串返回
问题描述
我有一个本地存储的包装器。
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”。任何想法?
解决方案
推荐阅读
- javascript - 在 Angular 9 中更新 CLI 生成的材料表的内容
- sql - 在 SQL Server 中:使用每个日期作为自己的列创建数据透视表
- python - 在networkx中找到最长的圆路
- java - 使用 ProcessBuilder 结果填充 JavaFX listView
- scala - 处理数据倾斜而不加盐火花中的连接键
- tensorflow - GlobalAvgPool1D 与输出大小不兼容
- swift - 如何使 Rectangle() 和 DatePicker 重叠?
- node.js - 打字稿:'数字| undefined' 不可分配给“number”类型的参数
- java - java URLConnection - 如何从包含大写字母的 URL 中读取数据?
- reactjs - 使用 react-i18next 补水 SSR React App 导致闪烁