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 ...
随机推荐
- 蓝牙secure simple pair 概述
Secure Simple Pairing,简称SSP,其流程主要分为六个部分: • IO capabilities exchange • Public key exchange • Authenti ...
- 02 Django REST Framework 序列化
01-创建序列化类 # 方式一: publish_list = models.Publish.objects.all() # 导入序列化组件 from django.core import seria ...
- 分布式存储ceph——(4)ceph 添加/删除osd
一.添加osd: 当前ceph集群中有如下osd,现在准备新添加osd:
- ios之库Protobuf的使用
https://blog.csdn.net/dangbai01_/article/details/81099001 (1)Protobuf是什么? Protobuf 即 google protocol ...
- UnityInspector显示扩展
比如经常在三方插件中看到如下在Inspector中的样式 这种对特别是要做编辑序列化数据脚本操作很友好,但是这个是如何实现呢?比如我们要创建一个保存序列化的npc基本数据,名字(Name),性别(Se ...
- AOP - 2 实例(SpringBoot 注解方式)
1.创建Spring Boot项目 创建一个Spring Boot 项目,然后pom中引入web 模块与AOP相关依赖. <dependency> <groupId>org.s ...
- python_正则表达式随笔
webpage_regex = re.search(r'span_ed7[\s\S]*', dd) [\s\S]* 匹配多行,转义字符 webpage_regex = re.compile('< ...
- Git学习指北
learnGitBranching:一个可视化学习 git 的网站 learngitbranching.js.org,虽然项目有些悠久,如果学习 git 的话可以来玩下
- Linux学习之路3-HelloWorld
1.window系统上创建helloworld.c文件,并编写程序 #include <stdio.h> main(){ printf("Hello World!"); ...
- 基于.NET平台的Ocelot网关框架教程汇总
Ocelot 框架是基于.NET 开发的 API 网关,API网关是系统内部服务暴露在外部的一个访问入口,类似于代理服务器,就像一个公司的门卫承担着寻址.限制进入.安全检查.位置引导等工作,我们可以形 ...