如何禁用特定行的ts规则?

如何禁用特定行的ts规则?

问题描述:

Summernote是一个jQuery插件,我不需要它的类型定义。我只是想修改对象,但TS不断抛出错误。下面的线条仍然给我:“属性'summernote'在'jQueryStatic'类型上不存在。”错误。如何禁用特定行的ts规则?

(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" 
    ] 
} 

您可以使用/* tslint:disable-next-line */在本地禁用tslint。但是,由于这是编译器错误,禁用tslint可能无济于事。

您随时都可以临时投$any

delete (<any>$).summernote.options.keyMap.pc.TAB 

,这将允许您访问任何你想要的属性。

+0

这是唯一结束了工作。谢谢! – Amir

类似this answer,您可以覆盖JQueryStatic类型定义为包括summernote属性。

interface JQueryStatic { 
    // Opt out of type-checking summernote using the any type 
    // See https://www.typescriptlang.org/docs/handbook/basic-types.html#any 
    summernote: any 
} 

(function ($) { 

    delete $.summernote.options.keyMap.pc.TAB; 
    delete $.summernote.options.keyMap.mac.TAB; 

})(jQuery) 
+0

在问我的问题之前,我确实发现了这一点,但这也没有奏效。这就像JQueryStatic没有被扩展,尽管声明。 – Amir

+0

这些是我测试我的答案的步骤: *全局安装TypeScript - 'npm install -g typescript' *在一个新目录中,运行'npm init -y'创建一个'package.json' *安装JQuery - 'npm install jquery --save' *安装JQuery类型定义 - 'npm install @ types/jquery --save-dev' *创建一个默认的'tsconfig.json'文件 - 'tsc --init' *创建一个TypeScript文件 - 'touch index.ts' *将我的答案的内容粘贴到'index.ts'中 *执行'tsc index.ts' - TypeScript成功编译该文件 @Amir请问您可以分享更多你的TypeScript配置? – macklinu