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. 浅析RPC远程过程调用基本原理

    在校期间大家都写过不少程序,比如写个hello world服务类,然后本地调用下,如下所示.这些程序的特点是服务消费方和服务提供方是本地调用关系. 而一旦踏入公司尤其是大型互联网公司就会发现,公司的系 ...

  2. Linux(Ubuntu)使用日记------Mongodb的安装与使用

    1.安装 Linux下安装mongodb还是比较容易的 直接使用apt-get安装即可,命令如下: sudo apt-get install mongodb 安装完成之后进行检验, “mongo sh ...

  3. JSP元素介绍

    1.HTML注释 <!--这个HTML注释--> 2.JSP注释 <% --这是JSP注释-- %> 3.声明 用于在页面中定义变量或方法 <% ! String use ...

  4. Tomcat启动报错,报找不到gdk_custom.jar

    在 tomcat/conf/context.xml 中新增如下配置 <Context> ... <JarScanner scanManifest="false"/ ...

  5. ImageMagick 安装 window10与错误总结

    一般默认安装最新的版本(目前是7.0.8),但是通过测试发现这个版本会报错(ImportError: MagickWand shared library not found.)所以建议安装 im6版本 ...

  6. 数据分析三剑客之Matplotlib

    Matplotlib绘图和可视化 简介 我的前面两篇文章介绍了 Nimpy ,Pandas .今天来介绍一下Matplotlib. 简单来说,Matplotlib 是 Python 的一个绘图库.它包 ...

  7. 用 hugo 和 netlify 搭建blog【转】

    用 hugo 和 netlify 搭建blog - kok的笔记本 Releases · gohugoio/hugo · GitHub 测试baidu 测试163

  8. [洛谷P1419] 寻找段落

    一道单调队列的好题 传送门:>Here< 题意:求一个连续子段,其长度在\([S,T]\)之间,使其平均值最大.保留三位小数 解题思路 考虑二分答案,转化为判定问题.设当前二分到\(k\) ...

  9. 机器学习---感知机(Machine Learning Perceptron)

    感知机(perceptron)是一种线性分类模型,通常用于二分类问题.感知机由Rosenblatt在1957年提出,是神经网络和支持向量机的基础.通过修改损失函数,它可以发展成支持向量机:通过多层堆叠 ...

  10. python学习day15 模块(重点)

    模块(重点) python2,与py3的区别 py2:range() 在内存中立即把所有的值都创建,xrange() 不会再内存中立即创建,而是在循环时边环边创建. py3:range() 不会再内存 ...