Summernote 是一个 jQuery 插件,我不需要它的类型定义。我只想修改对象,但 TS 一直抛出错误。下面的行仍然给我:“属性‘summernote’在类型‘jQueryStatic’上不存在。”错误。
(function ($) {
/* tslint:disable */
delete $.summernote.options.keyMap.pc.TAB;
delete $.summernote.options.keyMap.mac.TAB;
/* tslint:enable */
})(jQuery)
编辑:
这是我的 tsconfig.json
{
"compilerOptions": {
"outDir": "./dist/",
"sourceMap": true,
"noImplicitAny": true,
"module": "commonjs",
"target": "es5",
"allowJs": true,
"noUnusedParameters": true
},
"include": [
"js/**/*"
],
"exclude": [
"node_modules",
"**/*.spec.ts"
]
}
从 Typescript 2.6 开始,您现在可以绕过特定行的编译器错误/警告:
if (false) {
// @ts-ignore: Unreachable code error
console.log("hello");
}
请注意,官方文档“建议您非常谨慎地使用[此]”。几乎总是更可取的是转换为
any
,因为这样可以更好地表达意图。
旧答案:
您可以使用
/* tslint:disable-next-line */
在本地禁用 tslint。但是,由于这是一个编译器错误,禁用 tslint 可能没有帮助。
您随时可以临时将
$
投射到 any
:
delete ($ as any).summernote.options.keyMap.pc.TAB
这将允许您访问您想要的任何属性。
@ts-expect-error
TypeScript 3.9引入了新的魔法注释。
@ts-expect-error
将会:
@ts-ignore
if (false) {
// @ts-expect-error: Let's ignore a compile error like this unreachable code
console.log("hello"); // compiles
}
// If @ts-expect-error didn't suppress anything at all, we now get a nice warning
let flag = true;
// ...
if (flag) {
// @ts-expect-error
// ^~~~~~~~~~~~~~~^ error: "Unused '@ts-expect-error' directive.(2578)"
console.log("hello");
}
@ts-ignore
和 @ts-expect-error
就像编译错误的大锤。对于大多数情况,TypeScript 开发人员推荐更细粒度、更窄范围的类型系统解决方案:
我们添加 ts-ignore 的目的是让它用于剩余 5% 的内容,这些内容不能被任何现有的类型系统机制抑制 [...] 你的代码库中应该有非常非常少的
s [ .] - microsoft/TypeScript#19139ts-ignore
[...] 从根本上来说,我们认为您根本不应该在 TypeScript 中使用抑制。如果是类型问题,您可以对其进行强制转换(这就是存在
、强制转换和速记模块声明的原因)。如果这是一个语法问题,那么一切都会很糟糕,无论如何我们都会被破坏,所以抑制不会做任何事情(抑制不会影响解析错误)。 - microsoft/TypeScript#19573any
any
类型
// type assertion for single expression
delete ($ as any).summernote.options.keyMap.pc.TAB;
// new variable assignment for multiple usages
const $$: any = $
delete $$.summernote.options.keyMap.pc.TAB;
delete $$.summernote.options.keyMap.mac.TAB;
▶ 增强
JQueryStatic
界面
// ./global.d.ts
interface JQueryStatic {
summernote: any;
}
// ./main.ts
delete $.summernote.options.keyMap.pc.TAB; // works
在其他情况下,简写声明/增强是编译没有/可扩展类型的模块的方便实用程序。一个可行的策略也是增量迁移到TypeScript,通过
.js
和allowJs
编译器标志将尚未迁移的代码保留在checkJs: false
中。
您可以在该行之前简单地使用以下内容:
// @ts-ignore
如果您使用
eslint
执行检查或修复,您可以通过将其添加到行顶部来禁用该行
// eslint-disable-next-line @typescript-eslint/<RELEVANT_ESLINT_RULE>
我遇到的一个有趣的警告是,当需要为 JSX 的特定行禁用 TS 时,通常注释应该包含在大括号内,例如`{/* @ts-expect-error */}。这是一个有问题的案例:
// Cannot place it here either
<MyComponent
{/* @ts-expect-error */} // <== This will produce Unused '@ts-expect-error' directive.
someProps={withTsIssue}
// other props continue below, so we cannot make it a one line
// especially with particular eslint/prettier configurations
/>
解决方法是:
<MyComponent
// @ts-expect-error
someProps={withTsIssue}
// other props continue below, so we cannot make it a one line
// especially with particular eslint/prettier configurations
/>
在您的
tsconfig.json
中只需添加:
{
"downlevelIteration": true,
"strictNullChecks": false,
}
你会没事的。