JSDoc/JavaScript 语言服务:如何注释表达式? (如何投射)
JSDoc/JavaScript Language Service: how to annotate an expression? (how to cast)
我正在使用 Visual Studio 代码,JavaScript 语言服务配置为:
{
"compilerOptions": {
"checkJs": true
}
}
而且我找不到投射东西的方法,就像这里:
上面的例子应该在 Closure Compiler 中工作(未验证),如 documented here。但是我找不到 JavaScript 语言服务的等效语法。
我还尝试了以下更简单的语句,它也不起作用:
let castedWindow = (/** @type {any} */(window)); // castedWindow: Window (I want `any`)
我问的是如何进行强制转换,以及是否有人碰巧知道语法的记录位置(或者,如果没有记录,那么它是如何工作的)。
我问的可能吗?
感谢您的宝贵时间!
截至 TypeScript 2.5, support for type assertion/cast syntax in checkJs/@ts-check mode 已推出。
Type assertion/cast syntax in checkJs
/@ts-check
mode
TypeScript 2.5 introduces the ability to assert the type of
expressions when using plain JavaScript in your
projects. The
syntax is an /** @type {...} */
annotation comment followed by a
parenthesized expression whose type needs to be re-evaluated. For
example:
var x = /** @type {SomeType} */ (AnyParenthesizedExpression);
我正在使用 Visual Studio 代码,JavaScript 语言服务配置为:
{
"compilerOptions": {
"checkJs": true
}
}
而且我找不到投射东西的方法,就像这里:
上面的例子应该在 Closure Compiler 中工作(未验证),如 documented here。但是我找不到 JavaScript 语言服务的等效语法。
我还尝试了以下更简单的语句,它也不起作用:
let castedWindow = (/** @type {any} */(window)); // castedWindow: Window (I want `any`)
我问的是如何进行强制转换,以及是否有人碰巧知道语法的记录位置(或者,如果没有记录,那么它是如何工作的)。
我问的可能吗?
感谢您的宝贵时间!
截至 TypeScript 2.5, support for type assertion/cast syntax in checkJs/@ts-check mode 已推出。
Type assertion/cast syntax in
checkJs
/@ts-check
modeTypeScript 2.5 introduces the ability to assert the type of expressions when using plain JavaScript in your projects. The syntax is an
/** @type {...} */
annotation comment followed by a parenthesized expression whose type needs to be re-evaluated. For example:var x = /** @type {SomeType} */ (AnyParenthesizedExpression);