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的更多相关文章

  1. react中使用typescript时,error: Property 'setState' does not exist on type 'Home'

    问题描述: 我在react中用typescript时,定义一个Home组件,然后在组件里用setState时会有这样一个报错:(如图)Property 'setState' does not exis ...

  2. React报错之Property 'X' does not exist on type 'HTMLElement'

    正文从这开始~ 总览 在React中,当我们试图访问类型为HTMLElement 的元素上不存在的属性时,就会发生Property 'X' does not exist on type 'HTMLEl ...

  3. React报错之Property 'value' does not exist on type 'HTMLElement'

    正文从这开始~ 总览 当我们试图访问一个类型为HTMLElement的元素上的value属性时,会产生"Property 'value' does not exist on type 'HT ...

  4. React报错之Property 'value' does not exist on type EventTarget

    正文从这开始~ 总览 当event参数的类型不正确时,会产生"Property 'value' does not exist on type EventTarget"错误.为了解决 ...

  5. 解决TS报错Property 'style' does not exist on type 'Element'

    在使用queryselector获取一个dom元素,编译时却报错说property 'style' does not exist on type 'element'. 原因:这是typescript的 ...

  6. 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 ...

  7. 在angular项目中使用bootstrap的tooltip插件时,报错Property 'tooltip' does no t exist on type 'JQuery<HTMLElement>的解决方法和过程

    在angular4的项目中需要使用bootstrap的tooltip插件. 1. 使用命令安装jQuery和bootstrap npm install bootstrap jquery --save ...

  8. 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 | ...

  9. 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 ...

随机推荐

  1. JS Jquery 中 的遍历

      $.each()和$().each(),以及forEach()的用法   1.forEach是js中遍历数组的方法,如下 var arr=[1,2,3,4];arr.forEach(functio ...

  2. Python-异常处理-66

    异常和错误 # 1/0 # name # 2+'3' # [][3] #{}['k'] try: ') # 1/0 ') # name # 2+'3' # [][3] # {}['k'] ret = ...

  3. Net core 关于缓存的实现

    在很多项目中, 需要用到缓存,借鉴网上前辈们的一些经验,自己再进行总结简化了一些, 做出如下的缓存操作,其中包含内存缓存(IMemoryCache) 和 Redis 缓存; 一.前提内容, 导入两个包 ...

  4. JS 输入框输入数字检查

    <input id='ApplyInputNum' type='text' class='mytext form-control' align='left' onblur='InputCheck ...

  5. 微信支付之01------获取订单微信支付二维码的接口------Java实现

    [ 前言:以前写过一个获取微信二维码支付的接口,发现最近公司新开的项目会经常用到,现在我又翻出代码看了一遍,觉得还是把整个代码流程记下来的好 ] 借鉴博客: 他这篇博客写得不错,挺全的:https:/ ...

  6. IIS7下设置https主机名灰色无法修改

    打开iis绑定域名时,点击绑定弹出绑定框,在选择类型为“https”的时候,主机名为灰色的无法填写 解决方法: 1. 选择https,在选择相应的SSL证书,点击确定 2. 打开C:\Windows\ ...

  7. Python——代码汇总

    1.三级菜单 2.Windows启动服务 3.常用的Python实现 4.字典的基本操作

  8. memcache+tp3.2实现消息队列

    ){)){ ){));} $this->unLock();$this->resetSide('A');$this->resetSide('B');return true;} /* * ...

  9. [leetcode] 5.Longest Palindromic Substring-1

    开始觉得挺简单的 写完发现这个时间超限了: class Solution: def longestPalindrome(self, s: str) -> str: # longest palin ...

  10. 【洛谷P3014】Cow Line

    题目大意:康托展开和逆康托展开模板题. 题解: 注:20!约为 2e18. 代码如下 #include <bits/stdc++.h> using namespace std; const ...