如何禁用特定行的 ts 规则?
How to disable a ts rule for a specific line?
Summernote 是一个 jQuery 插件,我不需要它的类型定义。只想修改对象,TS一直报错。下面的行仍然给我:"Property 'summernote' does not exist on type 'jQueryStatic'." error.
(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 开始,您现在可以针对特定行绕过编译器 error/warning:
if (false) {
// @ts-ignore: Unreachable code error
console.log("hello");
}
注意the official docs "recommend you use [this] very sparingly"。几乎 总是 更好地转换为 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");
}
TypeScript 开发人员推荐什么?
@ts-ignore
和 @ts-expect-error
就像是编译错误的大锤。对于大多数情况,TypeScript 开发人员推荐更细粒度、范围更窄的类型系统解决方案:
We added ts-ignore with the intent that it be used for the remaining 5% that can't be suppressed by any existing type system mechanics [...] there should be very very very few ts-ignore
s in your codebase[.] - microsoft/TypeScript#19139
[...] fundamentally, we believe you shouldn't be using suppressions in TypeScript at all. If it's a type issue, you can cast out of it (that's why any
, casting, and shorthand module declarations exist). If it's a syntax issue, everything is awful and we'll be broken anyway, so suppressions won't do anything (suppressions do not affect parse errors). - microsoft/TypeScript#19573
问题案例的备选方案
▶ 使用 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;
▶ Augment JQueryStatic
界面
// ./global.d.ts
interface JQueryStatic {
summernote: any;
}
// ./main.ts
delete $.summernote.options.keyMap.pc.TAB; // works
在其他情况下,shorthand declarations / augmentations are handy utilities to compile modules with no / extendable types. A viable strategy is also to incrementally migrate to TypeScript, keeping not yet migrated code in .js
via allowJs
and checkJs: false
编译器标志。
您可以在该行之前简单地使用以下内容:
// @ts-ignore
如果您正在使用 eslint
执行检查或修复,您可以通过将其添加到该行的顶部来禁用该行
// eslint-disable-next-line @typescript-eslint/<RELEVANT_ESLINT_RULE>
Summernote 是一个 jQuery 插件,我不需要它的类型定义。只想修改对象,TS一直报错。下面的行仍然给我:"Property 'summernote' does not exist on type 'jQueryStatic'." error.
(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 开始,您现在可以针对特定行绕过编译器 error/warning:
if (false) {
// @ts-ignore: Unreachable code error
console.log("hello");
}
注意the official docs "recommend you use [this] very sparingly"。几乎 总是 更好地转换为 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");
}
TypeScript 开发人员推荐什么?
@ts-ignore
和 @ts-expect-error
就像是编译错误的大锤。对于大多数情况,TypeScript 开发人员推荐更细粒度、范围更窄的类型系统解决方案:
We added ts-ignore with the intent that it be used for the remaining 5% that can't be suppressed by any existing type system mechanics [...] there should be very very very few
ts-ignore
s in your codebase[.] - microsoft/TypeScript#19139
[...] fundamentally, we believe you shouldn't be using suppressions in TypeScript at all. If it's a type issue, you can cast out of it (that's why
any
, casting, and shorthand module declarations exist). If it's a syntax issue, everything is awful and we'll be broken anyway, so suppressions won't do anything (suppressions do not affect parse errors). - microsoft/TypeScript#19573
问题案例的备选方案
▶ 使用 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;
▶ Augment JQueryStatic
界面
// ./global.d.ts
interface JQueryStatic {
summernote: any;
}
// ./main.ts
delete $.summernote.options.keyMap.pc.TAB; // works
在其他情况下,shorthand declarations / augmentations are handy utilities to compile modules with no / extendable types. A viable strategy is also to incrementally migrate to TypeScript, keeping not yet migrated code in .js
via allowJs
and checkJs: false
编译器标志。
您可以在该行之前简单地使用以下内容:
// @ts-ignore
如果您正在使用 eslint
执行检查或修复,您可以通过将其添加到该行的顶部来禁用该行
// eslint-disable-next-line @typescript-eslint/<RELEVANT_ESLINT_RULE>