如何在TypeScript中使用 “as” 语法

huangapple go评论81阅读模式
英文:

How to use "as" syntax in TypeScript

问题

我正在开发一个Angular项目。它运行得很完美,但它未通过Linting测试。这个问题并没有提供很大的帮助。这是我收到的错误信息:

错误: C:/Users/.../monthpicker.component.ts:164:22 - 使用'<>'语法的类型断言是被禁止的。请使用'as'语法。
在列出的文件中发现Lint错误。

而这个错误出现在我的TypeScript文件的第164行:

testMethod() {
    this.text = (<HTMLInputElement>document.getElementById('rangeInput')).value;
}

我是Angular的新手,不知道该怎么办。我不能删除&lt;&gt;,因为rangeInput是我的HTML中的一个输入元素:

<input id="rangeInput" [ngModel]="text" placeholder="Select range" />

请帮助我,如何修复这个问题并通过Linting测试。我正在使用Node v10.15.3

PS:在项目运行时没有任何错误或警告。只有在git push时开始Linting测试时才会出现这个问题。

英文:

I'm working on an angular project. It is working perfectly. But it is failing a linting test. This question wasn't very helpful. This the error I'm getting:

ERROR: C:/Users/.../monthpicker.component.ts:164:22 - Type assertion using the &#39;&lt;&gt;&#39; syntax is forbidden. Use the &#39;as&#39; syntax instead.
Lint errors found in the listed files.

And that line 164 is in my ts file:

testMethod() {
    this.text = (&lt;HTMLInputElement&gt;document.getElementById(&#39;rangeInput&#39;)).value;
}

I'm new to Angular. I don't know what to do. I cant remove &lt;&gt; because rangeInput is an input element in my HTML:

&lt;input id=&quot;rangeInput&quot; [ngModel]=&quot;text&quot; placeholder=&quot;Select range&quot; /&gt;

Please help me. How to fix this and pass this linting test. I'm using Node v10.15.3.

PS: there is no error or warning when this project runs. It happens only during git push when it starts linting tests.

答案1

得分: 4

我尚未测试过这个,但通常的语法应该是:

testMethod() {
    this.text = (document.getElementById('rangeInput') as HTMLInputElement).value;
}

试试看吧。

英文:

I've not tested this, but usually the syntax would be;

testMethod() {
    this.text = (document.getElementById(&#39;rangeInput&#39;) as HTMLInputElement).value;
}

Give that a shot.

答案2

得分: 1

// 当独立使用时

// 在语句之前添加
// -/ StatementThatResolves();
const myCanvas1 = document.getElementById("something");
// 在语句之后添加
// -/ StatementThatResolves() as intendedType;
const myCanvas2 = document.getElementById("something") as HTMLCanvasElement;

// 当需要分组时使用 ()

// 在语句之前添加
// -/ StatementThatResolves();
const myCanvasHasWidth1 = (document.getElementById("something")).hasAttribute("width");
// 在语句之后添加
// -/ StatementThatResolves() as intendedType;
const myCanvasHasWidth2 = (document.getElementById("something") as HTMLCanvasElement).hasAttribute("width");

英文:
// When stand Alone

// To add before statement
// -/ &lt;intendedType&gt;StatementThatResolves();
const myCanvas1 = &lt;HTMLCanvasElement&gt;document.getElementById(&quot;something&quot;);
// To add after statement
// -/ StatementThatResolves() as intendedType;
const myCanvas2 = document.getElementById(&quot;something&quot;) as HTMLCanvasElement;

// When you need to group use ()

// To add before statement
// -/ &lt;intendedType&gt;StatementThatResolves();
const myCanvasHasWidth1 = (&lt;HTMLCanvasElement&gt;document.getElementById(&quot;something&quot;)).hasAttribute(&quot;width&quot;);
// To add after statement
// -/ StatementThatResolves() as intendedType;
const myCanvasHasWidth2 = (document.getElementById(&quot;something&quot;) as HTMLCanvasElement).hasAttribute(&quot;width&quot;);

For better clarity, try to standardise the casting method you like using linting rules.

答案3

得分: 1

I don't think you even need the "as".
你甚至不需要 "as"。

Did you try:

testMethod() {
this.text = (document.getElementById('rangeInput')).value;
}
你尝试过这样吗:

testMethod() {
this.text = (document.getElementById('rangeInput')).value;
}

Or as in here

或者如这里

const el: HTMLElement = document.getElementById('content');

const el: HTMLElement = document.getElementById('content');

Though if you just need to pass a value to the method just use

虽然如果你只需要传递一个值给方法,只需使用

<input id="rangeInput" [ngModel]="text" placeholder="Select range" (change)="testMethod($event)" />

<input id="rangeInput" [ngModel]="text" placeholder="Select range" (change)="testMethod($event)" />

testMethod(event: any) {
this.text = event.target.value;
}

testMethod(event: any) {
this.text = event.target.value;
}

Notice that you can use any other event that input supports to trigger the method(besides change as in this example)

请注意,除了此示例中的更改事件之外,您还可以使用输入支持的任何其他事件来触发该方法。

Or just make use of two-way binding as found here

或者只需使用在这里找到的双向绑定

<input id="rangeInput" [(ngModel)]="text" placeholder="Select range"/>

<input id="rangeInput" [(ngModel)]="text" placeholder="Select range"/>

const text: string;

const text: string;

英文:

I don't think you even need the "as".
Did you try:

testMethod() {
    this.text = (document.getElementById(&#39;rangeInput&#39;)).value;
}

Or as in here

> const el: HTMLElement = document.getElementById('content');

Though if you just need to pass a value to the method just use

&lt;input id=&quot;rangeInput&quot; [ngModel]=&quot;text&quot; placeholder=&quot;Select range&quot; (change)=&quot;testMethod($event)&quot; /&gt;

testMethod(event: any) {
    this.text = event.target.value;
}

Notice that you can use any other event that input supports to trigger the method(besides change as in this example)

Or just make use of two way binding as found here

&lt;input id=&quot;rangeInput&quot; [(ngModel)]=&quot;text&quot; placeholder=&quot;Select range&quot;/&gt;

const text: string;

答案4

得分: 0

我强烈建议您删除 document.getElementById,因为当您的项目变得更加复杂时,它可能会带来很多问题,因为它完全独立于 Angular。

如果您想访问您的 rangeInput,您可以将其发送到您的函数。首先给它一个 Angular 的 id:

<input id="rangeInput" #angularIdExample [ngModel]="text" placeholder="选择范围" />

然后将它发送到您的方法。这里我使用了一个按钮:

<button (click)="testMethod(angularIdExample)">testMethod</button>

然后您可以在您的方法中访问它并为它指定正确的类型:

testMethod(inputElement: HTMLInputElement) {
    this.text = inputElement.value;
}

示例:https://stackblitz.com/edit/angular-sending-input-reference

英文:

I would strongly recommend that you deletes document.getElementById because it could bring a lot of problems when your project become more complex because it is complete outside angular.

If you want to access your rangeInput you can just send it to your function. First give it an angular id:

&lt;input id=&quot;rangeInput&quot; #angularIdExample [ngModel]=&quot;text&quot; placeholder=&quot;Select range&quot; /&gt;

and send it to your method. Here I am using a button

&lt;button (click)=&quot;testMethod(angularIdExample)&quot;&gt;testMethod&lt;/button&gt;

then you can access it in your method giving it the correct type

testMethod(inputElement: HTMLInputElement) {
    this.text = inputElement.value;
  }

Example: https://stackblitz.com/edit/angular-sending-input-reference

huangapple
  • 本文由 发表于 2020年1月3日 18:57:43
  • 转载请务必保留本文链接:https://go.coder-hub.com/59577340.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定