property xxx does not exist on type Object报错
在TypeScript中如果按JS的方式去获取对象属性,有时会提示形如Property ‘xxx’ does not exist on type 'Object’的错误。下文代码中的’xxx’替换成你的变量。
var obj: Object = Object.create(null);
obj.xxx= "xxx";//[ts] Property 'xxx' does not exist on type'Object'.
这是因为Typescript在执行代码检查时在该对象没有定义相应属性,遇到该报错有以下几种解决方式:
1、将对象类型设置为any
这是是一种非常效率的解决办法,可以访问修改任何属性不会出现编译错误。
var obj: any = Object.create(null);
obj.xxx= "xxx";
2、通过字符方式获取对象属性
这种方式有些强行的感觉,但是依然能解决编译错误的问题。
var obj: Object = Object.create(null);
obj["xxx"] = "xxx";
3、通过接口定义对象所具有的属性
虽然较为繁琐,但却是最提倡的一种解决方式。通过接口声明对象后,所具有的属性值一目了然。
var obj: xxxObject = Object.create(null);
obj.xxx= "xxx";interface xxxObject {xxx?: string
}
4、使用断言强制执行
声明断言后,编译器会按断言类型去执行。
var obj: Object = Object.create(null);
(obj as any).xxx= "xxx";
property xxx does not exist on type Object报错相关推荐
- Property 'filter' does not exist on type 'Observable' 报错解决方法
我想做的是监听地址参数变化: import 'rxjs/operator/filter';//...this.router.events.filter((event:Event) => even ...
- 完美解决:Property ‘XXX‘ does not exist on type ‘Window‘
发生情景: 在app中的h5,app注入了全局的window对象和webview里面的页面交互,打包的时候报了错误. 解决方式: 方法一: (window as any).xxx 方法二: decla ...
- TypeScript Property ‘XXX‘ does not exist on type ‘never‘.
开发过程中出现这个错误是因为Typescript在执行代码检查时在该对象没有定义相应属性,这个错误不致命,遇到该错误有以下几种解决办法. 1,将对象设置成 any this.targetArray = ...
- Property ‘xxx‘ does not exist on type ‘{ xxx(file: any, fileList: any): void;的报错
关闭tsconfig.json里的"strict",把 true 改为 false 即可
- TypeScript 错误property does not exist on type Object
在TypeScript中如果按JS的方式去获取对象属性,有时会提示形如Property 'value' does not exist on type 'Object'的错误.具体代码如下: var o ...
- 响应式编程时出现:error TS2339: Property 'debounceTime' does not exist on type 'Observableany'.
constructor() {this.searchInput.valueChanges//.debounceTime(500) 无法使用.pipe(debounceTime(1000)).subsc ...
- Angular响应式开发中报错Property 'map' does not exist on type 'Observable'.引用rxjs也没用。
Angular响应式开发源代码如下: import { Component, OnInit } from '@angular/core'; import {Observable} from 'rxjs ...
- Property ‘next‘ does not exist on type ‘Component<any, {}, any>‘问题的解决方法
场景:使用antd的Carousel组件时,自定义左右切换按钮,触发组件的next(),prev()方法时报错 错误写法: handleNext(){this.refs.img.next()}< ...
- TS2550: Property ‘entries‘ does not exist on type ‘ObjectConstructor‘.
问题:TS2550: Property 'entries' does not exist on type 'ObjectConstructor'. Do you need to change your ...
最新文章
- 危害企业IT系统最严重的五个安全威胁
- 如何更改jupyter notebook默认存储路径
- Android stadio 模板 liveTemplate不管用
- 完美刷机显示云服务器,完美刷机显示云服务器
- C++ Primer ----- 关于初始化
- java svg 读取dom结构_SVG基础以及使用Javascript DOM操作SVG
- 开源的,跨平台的.NET机器学习框架ML.NET
- IAR切BANK--命令连接器文件xcl格式说明
- 算法题 如何找到数组中重复的数字
- java文件与bean所定义的_Spring定义bean的三种方式和自动注入
- go mysql 多并发_MySQL并发处理-Go语言中文社区
- mysql如何对字段加密_MySQL对指定字段进行加密(双向加密)
- 为了找出最好用的安卓模拟器,我发起了一项众测
- 逆向四、JEB的使用
- 内存继续涨价 LPDDR4/LPDDR4X内存标准升级
- 怎样才能叫高级程序员?
- 世界顶级机器学习科学家黄恒加入京东,出任京东大数据首席科学家
- Spark内核设计的艺术:架构设计与实现——前言
- C语言自学完备手册(07)——printf()输出格式详解
- 我的世界服务器显示未知指令,我的世界输入创造指令为什么显示未知指令