TypeScript 错误property does not exist on type Object
TypeScript 错误property does not exist on type Object
在TypeScript中如果按JS的方式去获取对象属性,有时会提示形如Property 'value' does not exist on type 'Object'的错误。具体代码如下:
var obj: Object = Object.create(null);
obj.value = "value";//[ts] Property 'value' does not exist on type'Object'.
这是因为Typescript在执行代码检查时在该对象没有定义相应属性,遇到该报错有以下几种解决方式:
1.将对象类型设置为any
这是是一种非常效率的解决办法,可以访问修改任何属性不会出现编译错误。具体代码如下:
var obj: any = Object.create(null);
obj.value = "value";
2.通过字符方式获取对象属性
这种方式有些hack的感觉,但是依然能解决编译错误的问题。具体代码如下:
var obj: Object = Object.create(null);
obj["value"] = "value";
3.通过接口定义对象所具有的属性
虽然较为繁琐,但却是最提倡的一种解决方式。通过接口声明对象后,所具有的属性值一目了然。具体代码如下:
var obj: ValueObject = Object.create(null);
obj.value = "value";
interface ValueObject {
value?: string
}
4.使用断言强制执行
声明断言后,编译器会按断言类型去执行。具体代码如下:
var obj: Object = Object.create(null);
(obj as any).value = "value";
TypeScript 错误property does not exist on type Object的更多相关文章
- react中使用typescript时,error: Property 'setState' does not exist on type 'Home'
问题描述: 我在react中用typescript时,定义一个Home组件,然后在组件里用setState时会有这样一个报错:(如图)Property 'setState' does not exis ...
- React报错之Property 'X' does not exist on type 'HTMLElement'
正文从这开始~ 总览 在React中,当我们试图访问类型为HTMLElement 的元素上不存在的属性时,就会发生Property 'X' does not exist on type 'HTMLEl ...
- React报错之Property 'value' does not exist on type 'HTMLElement'
正文从这开始~ 总览 当我们试图访问一个类型为HTMLElement的元素上的value属性时,会产生"Property 'value' does not exist on type 'HT ...
- React报错之Property 'value' does not exist on type EventTarget
正文从这开始~ 总览 当event参数的类型不正确时,会产生"Property 'value' does not exist on type EventTarget"错误.为了解决 ...
- 解决TS报错Property 'style' does not exist on type 'Element'
在使用queryselector获取一个dom元素,编译时却报错说property 'style' does not exist on type 'element'. 原因:这是typescript的 ...
- Open quote is expected for attribute "property" associated with an element type "result".错误
java Mybatis 框架下的项目 报 Open quote is expected for attribute "property" associated with a ...
- 在angular项目中使用bootstrap的tooltip插件时,报错Property 'tooltip' does no t exist on type 'JQuery<HTMLElement>的解决方法和过程
在angular4的项目中需要使用bootstrap的tooltip插件. 1. 使用命令安装jQuery和bootstrap npm install bootstrap jquery --save ...
- Property 'validate' does not exist on type 'Element | Element[] | Vue | Vue[]'. Property 'valid...
使用vue-cli 3.0+Element-ui时候,调用form表单校验时候出现的问题是: Property 'validate' does not exist on type 'Element | ...
- javax.el.PropertyNotFoundException: Property 'name' not found on type java.lang.String
javax.el.PropertyNotFoundException: Property 'name' not found on type java.lang.String javax.el.Bean ...
随机推荐
- nextInt()和nextLine()一起使用时的注意点
问题原因:nextLine()会把nextInt(),next(),nextDouble(),nextFloat()的结束换行符作为字符串读入,进而不需要从键盘输入字符串nextLine便已经转向了下 ...
- filebeat-6.4.3-windows-x86_64输出Kafka
配置filebeat.yml文件 filebeat.prospectors: - type: log encoding: utf- enabled: true paths: - e:\log.log ...
- CSS绝对定位元素居中的几种方法
转载自-CSS居中绝对https://www.cnblogs.com/skura23/p/6530556.html 作者:PajamaCat 1,div宽度未知1 <body> <d ...
- 正则 re模块
Python 正则表达式 re 模块 简介 正则表达式(regular expression)是可以匹配文本片段的模式.最简单的正则表达式就是普通字符串,可以匹配其自身.比如,正则表达式 ‘hello ...
- Android技术文章收集
Android高工必备技能! 我的 Android 开发实战经验总结 Android开发在路上:少去踩坑,多走捷径 //微信 微信Android客户端架构演进之路 微信Android版智能心跳方案 / ...
- NOIP2018游记
day1 不想吐槽题目了... 40min写完,然后对拍+玩小恐龙. 每个人都阿克了. 预计得分:100+100+100 day2 体验极差 t1本来想写 \(O(n)\) 做法,写到一半发现可以 \ ...
- bzoj1444[Jsoi2009]有趣的游戏[AC自动机]
题面 bzoj 我要向师父学习善待每一只数据结构 考虑成环,那么高斯消元 然鹅这道题太小了 所以直接转移矩阵自乘就好啦 终点不向外连边 有一条向自己的,概率为一的自环来作为结尾 对于其他店 若有边\( ...
- 【BZOJ4032】[HEOI2015]最短不公共子串(后缀自动机,序列自动机)
[BZOJ4032][HEOI2015]最短不公共子串(后缀自动机,序列自动机) 题面 BZOJ 洛谷 题解 数据范围很小,直接暴力构建后缀自动机和序列自动机,然后直接在两个自动机上进行\(bfs\) ...
- <数据结构基础学习>(五)递归
一. 递归 本质上,将原来的问题,转化为更小的同样的问题 举例:数组求和 Sum(arr[0... n-1]) = arr[0] + Sum(arr[1...n-1]) 第二部分为更小的同一个问题 ...
- Java Web乱码原因与解决
Java Web乱码原因与解决 一.了解编码常识: 1.ASCII 码 众所周知,这是最简单的编码.它总共可以表示128个字符,0~31是控制字符如换行.回车.删 除等,32~126是打印字符,可以通 ...