问题描述
我正在使用Visual Studio Code,并将JavaScript语言服务配置为:
I'm using Visual Studio Code with JavaScript Language Service configured as:
{
"compilerOptions": {
"checkJs": true
}
}
而且我找不到一种投射内容的方法,例如:
And I can't find a way to cast something, like here:
以上示例应在Closure Compiler(未经验证)中工作,为此处记录.但是我找不到JavaScript语言服务的等效语法.
The example above should work in Closure Compiler (unverified), as documented here. But I can't find the equivalent syntax for JavaScript Language Service.
我还尝试了以下更简单的语句,该语句也不起作用:
I also tried with the following simpler statement, which does not work either:
let castedWindow = (/** @type {any} */(window)); // castedWindow: Window (I want `any`)
我要问的是如何进行强制类型转换,以及是否有人偶然知道语法在哪里记录(或者,如果没有记录,则只是知道它是如何工作的).
I'm asking how to do casts, and if someone happens to know where the syntax is documented (or, if undocumented, just how it works).
我要问的甚至有可能吗?
Is what I'm asking even possible?
感谢您的时间!
推荐答案
截至 TypeScript 2.5 ,支持在checkJs/@ ts-check模式下的类型声明/广播语法.
var x = /** @type {SomeType} */ (AnyParenthesizedExpression);
这篇关于JSDoc/JavaScript语言服务:如何注释表达式?(如何投射)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!