简体   繁体   English

打字稿在valueAsDate上引发错误

[英]Typescript throwing an error on valueAsDate

I have the following Javascript code: 我有以下Javascript代码:

var now = new Date();
now.setDate(now.getDate() + 7);
document.getElementById('txtCreateGroupExpirationDate').valueAsDate = now;

When I throw this code into a TypeScript file, TypeScript complains with the following error: 当我将此代码放入TypeScript文件中时,TypeScript出现以下错误:

Property 'valueAsDate' does not exist on type 'HTMLElement' 类型“ HTMLElement”上不存在属性“ valueAsDate”

My Javascript works fine and valueAsDate is a legal property, as defined by the Mozilla Developer Network . 根据Mozilla开发人员网络的定义,我的Javascript可以正常运行,并且valueAsDate是合法属性。 In addition, you can see that this property is defined in the Microsoft Typescript Core . 此外,您可以看到此属性是在Microsoft Typescript Core中定义的。

So why am I getting this error? 那我为什么会收到这个错误? I am using Typescript 2.0. 我正在使用Typescript 2.0。

valueAsDate is only supported on HTMLInputElement , and TypeScript doesn't know what kind of element txtCreateGroupExpirationDate is. valueAsDate仅在HTMLInputElement受支持,并且TypeScript不知道txtCreateGroupExpirationDate是哪种元素。 Use a type assertion to change the type of the expression: 使用类型断言来更改表达式的类型:

(<HTMLInputElement>document.getElementById('txtCreateGroupExpirationDate')).valueAsDate = now;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM