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 ...
随机推荐
- boost学习目录
Boost之数值转换lexical_cast https://www.cnblogs.com/TianFang/archive/2013/02/05/2892506.html Boost之字符串算法s ...
- 自定义 js 文件的集成引用
这里的内容, 提前要知道 import comm from ‘...’ 和 import { comm } from ‘...’ 的基础知识. 我举个案例, 当你有很多api文件的时候, 比如 ...
- 小A的柱状图
链接 [https://ac.nowcoder.com/acm/contest/549/H] 题意 [] 分析 很显然你必须找到该高度下往左右找到第一个高度比该位置小的.这个区间的宽*该高度.就当前能 ...
- 在C++中定义常量
在 C++ 中,有两种简单的定义常量的方式: 使用 #define 预处理器. 使用 const 关键字 使用 #define 预处理器: #define identifier value: #inc ...
- jdbc连接字符串
MySQL:String Driver="com.mysql.jdbc.Driver"; //驱动程序String URL="jdbc:mysql://localhost ...
- iOS 打包.framework(包括第三方、图片、xib、plist文件)详细步骤及需要注意的地方
https://www.cnblogs.com/yk123/p/9340268.html // 加载自定义名称为Resources.bundle中对应images文件夹中的图片// 思路:从mainb ...
- Java的selenium代码随笔(6)
//获取元素列表public List<WebElement> ListElements(WebElement webElement, By parentBy, By childrenBy ...
- flutter 返回键监听
本篇为继上片监听返回键基础下优化: 以下做返回键监听两种情况: import 'package:fluttertoast/fluttertoast.dart'; //提示第三方插件 1. 单击提示双击 ...
- ArrayList如何扩容?
1.调用ArrayList的参构造方法,此时集合内部是一个空数组 transient Object[] elementData; private static final Object[] DEFAU ...
- delphi Ctrl+鼠标左键或者Find Declaration不能定位到源文件
在Delphi代码编辑器中使用Ctrl+鼠标左键可跳转到鼠标下的类所在的定义处,但今天发现一个奇怪的问题,EhLib组件的类无法跳转(包括uses中的pas文件),重新安装也是如此,后来经过验证,发现 ...