TypeScript & Object Error

Element implicitly has an 'any' type because expression of type 'any' can't be used to index type 'Object'.

Error


class StorageUtils {
// [x: string]: Object;
// key: string;;
store: Object;
constructor(store: object) {
this.store = store;
}
setData(key: string = ``, data: object) {
sessionStorage.setItem(key, JSON.stringify(data));
}
// string primitive
getData(key: string = ``) {
// this.store;
const obj = JSON.parse(sessionStorage.getItem(key));
}
// String Object
// getData(key: String = ``) {
// // this.store;
// const obj = JSON.parse(sessionStorage.getItem(key));
// }
clear(key: string) {
delete this.store[key];
}
clearAll() {
this.store = {};
}
init() {
this.store = {};
}
}

https://stackoverflow.com/questions/57086672/element-implicitly-has-an-any-type-because-expression-of-type-string-cant-b


OK

key keyof TypeObject

interface TypeObject {
key: string;
} class StorageUtils {
// [x: string]: Object;
store: TypeObject;
constructor(store: TypeObject) {
this.store = store;
}
setData(key: string = ``, data: object) {
sessionStorage.setItem(key, JSON.stringify(data));
}
// string primitive
getData(key: string = ``) {
// this.store;
const obj = JSON.parse(sessionStorage.getItem(key));
}
// String Object
// getData(key: String = ``) {
// // this.store;
// const obj = JSON.parse(sessionStorage.getItem(key));
// }
clear(key: keyof TypeObject) {
delete this.store[key];
}
clearAll() {
this.store = {
key: ``,
};
}
init() {
this.store = {
key: ``,
};
}
}

TypeScript 2.1

keyof

https://www.typescriptlang.org/docs/handbook/release-notes/typescript-2-1.html#keyof-and-lookup-types


xgqfrms 2012-2020

www.cnblogs.com 发布文章使用:只允许注册用户才可以访问!


TypeScript & Object Error的更多相关文章

  1. Nuxt / Vue.js in TypeScript: Object literal may only specify known properties, but 'components' does not exist in type 'VueClass'

    项目背景, Nuxt(vue), TypeScript 生成完项目框架, 添加测试demo页面. 在生成的模板代码中添加layout配置如下: <script lang="ts&quo ...

  2. [TypeScript] Represent Non-Primitive Types with TypeScript’s object Type

    ypeScript 2.2 introduced the object, a type that represents any non-primitive type. It can be used t ...

  3. git推送报错: No path specified. See 'man git-pull' for valid url syntax或does not appear to be a git repository以及remote: error: insufficient permission for adding an object to repository databa

    本地(windows)代码想推送到linux自己搭建的git服务端,第一步是建立本地与服务端的关联,第二步是本地推送到服务端. 第一步需要看你的本地工程是否从git上clone来的,如果是clone来 ...

  4. Error serializing object:序列化对象时出错

    序列化对象时出错 :Error serializing object. Error serializing object. Cause: java.io.NotSerializableExceptio ...

  5. TypeScript的概要和简介

    安装 Typescript的基本介绍可以自行百度 centos虚拟机中可以完整的体验, virtualbox安装开发版本,选择开发工具项,否则增强功能无法安装[提示kernel 头文件缺失,yum安装 ...

  6. Angular2中实现基于TypeScript的对象合并方法:extend()

    TypeScript里面没有现成的合并对象的方法,这里借鉴jQuery里的$.extend()方法.写了一个TypeScript的对象合并方法,使用方法和jQuery一样. 部分代码和jQuery代码 ...

  7. 利用Object.prototype.toString方法,实现比typeof更准确的type校验

    Object.prototype.toString方法返回对象的类型字符串,因此可以用来判断一个值的类型. 调用方法: Object.prototype.toString.call(value) 不同 ...

  8. 原生JS:Object对象详细参考

    Object对象详细参考 本文参考MDN做的详细整理,方便大家参考MDN JavaScript原生提供一个Object对象(注意起首的O是大写),所有其他对象都继承自这个对象. 构造函数: Objec ...

  9. SpringBoot接口服务处理Whitelabel Error Page(转)

    To switch it off you can set server.error.whitelabel.enabled=false http://stackoverflow.com/question ...

随机推荐

  1. springBoot controller入参LocalDateTime

    @JsonFormat(shape = JsonFormat.Shape.STRING, pattern="yyyy-MM-dd HH:mm:ss") @DateTimeForma ...

  2. 用tqdm和rich为固定路径和目标的python算法代码实现进度条

    适用场景 在存在固定长度的算法中可以可视化算法执行的过程,比如对一个固定长度的数组的遍历,就是一种适合使用进度条来进行可视化的场景.而一些条件循环,比如while循环,不一定适合使用进度条来对算法执行 ...

  3. luoguP6754 [BalticOI 2013 Day1] Palindrome-Free Numbers

    目录 luoguP6754 [BalticOI 2013 Day1] Palindrome-Free Numbers 简述题意: Solution: Code luoguP6754 [BalticOI ...

  4. CF570B

    题意转化: 一条长为n的路,路上有一点m,问你在什么地方再设一点a可以使路上任意一点到点a的概率大于到点m的概率 所谓概率更大,也就是说从离点m更远的一端到点a的长度小于到点m的长度 (因为此长度内所 ...

  5. XCTF-基础Android

    前期工作 查壳,无.打开看是普通的输入注册码然后验证. 逆向分析 这题太简单了,就不贴图了.看Manifest得知MainActivity是启动类,也就是点进去输入注册码那个类.密码不重要,验证成功后 ...

  6. Java实现windows,linux服务器word,excel转为PDF;aspose-words,Documents4j

    Java实现windows,linux服务器word,excel转为PDF:aspose-words,Documents4j 一.通过aspose-words将word,Excel文档转为PDF 1. ...

  7. (八)整合 Dubbo框架 ,实现RPC服务远程调用

    整合 Dubbo框架 ,实现RPC服务远程调用 1.Dubbo框架简介 1.1 框架依赖 1.2 核心角色说明 2.SpringBoot整合Dubbo 2.1 核心依赖 2.2 项目结构说明 2.3 ...

  8. Jenkins免密码登录

    Jenkins免密码登录 相关内容原文地址: 简书:海边的卡夫卡丶:Jenkins免密码登录 昨天惊现一个神奇的问题,Jenkins无法登录,无论是初始化的账号密码,还是admin用户,都登录不进去了 ...

  9. Linux内存运维操作及常用命令

    Linux内存运维操作及常用命令 1.问题诊断 1.1 什么是 Linux 服务器 Load Average? 1.2如何查看 Linux 服务器负载? 1.3服务器负载高怎么办? 1.4如何查看服务 ...

  10. RedisUtil,Redis工具类

    RedisUtil,Redis工具类 1.配置maven,增加依赖 2.配置工具类 1.配置maven,增加依赖 <dependency> <groupId>redis.cli ...