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. Bloom Filter(布隆过滤器)如何解决缓存穿透

    本文摘抄自我的微信公众号"程序员柯南",欢迎关注!原文阅读 缓存穿透是什么? 关于缓存穿透,简单来说就是系统处理了大量不存在的数据查询.正常的使用缓存流程大致是,数据查询先进行缓存 ...

  2. dump解析入门-用VS解析dump文件进行排障

    突然有一天部署在服务器的一个应用挂掉了,没办法只能进入服务器打开 [事件查看器]查看下,好不容易找到了打开后一脸懵逼 事件查看器查到的内容根本对我们排障没有任何作用. 在这个时候如果有对应的dump文 ...

  3. 菜鸟学IT之第一次作业

    作业的要求来自于:https://www.cnblogs.com/greyzeng/p/9581624.html 反思· 为何要来上课并且认真参与? 在大学中的师生关系? 自我简述题目 心得· 学习态 ...

  4. 偶发异常BUG,如何高效精准分析排查定位?

    偶发异常BUG,如何高效精准分析排查定位? 作为测试,经常会收到领导.同事.用户反馈过来各种各样BUG,令人措手不及 首选需要判断确认是不是BUG,不要急于给予回复,需有充分的条件给予说明回复 很多测 ...

  5. php函数 array_count_values

    (PHP 4, PHP 5, PHP 7) array_count_values — 统计数组中所有的值 array_count_values ( array $array ) : array arr ...

  6. FJUTOJ-周赛2016-12-16

    注:fjutoj基本每周都有一次周赛,欢迎大家都来参加! 网址:http://59.77.139.92/index.jsp A题:来源 POJ 2773 题意:给两个数m和k,问第k 个和m 互素的数 ...

  7. 二叉树最近公共祖先(LeetCode)

    给定一个二叉树, 找到该树中两个指定节点的最近公共祖先. 百度百科中最近公共祖先的定义为:“对于有根树 T 的两个结点 p.q,最近公共祖先表示为一个结点 x,满足 x 是 p.q 的祖先且 x 的深 ...

  8. flex布局实例demo全解

    上篇文章介绍了Flex布局的语法,今天介绍常见布局的Flex写法. 你会看到,不管是什么布局,Flex往往都可以几行命令搞定. 我只列出代码,详细的语法解释请查阅<Flex布局教程:语法篇> ...

  9. python的局部变量,全局变量,类变量,实例变量

    定义: a.全局变量:在模块内.在所有函数外面.在class外面,这就是全局变量. b.局部变量:在函数内.在class的方法内(未加self修饰的),这就是局部变量. c. 静态变量:在class内 ...

  10. nginx的限流问题

    nginx的限流问题 http{ limit_req_zone $binary_remote_addr zone=req_one:10m rate=100r/s;server{ listen 8080 ...