如何修复SonarLint警告"Replace this union type with a type alias"(全局变量场景)
解决SonarLint「Replace this union type with a type alias」全局变量警告
- 先在全局作用域或全局声明文件(如
global.d.ts)中定义对应的类型别名:
type GlobalUnionType = string | number | null;
(把string | number | null替换成你实际用到的联合类型)
- 将全局变量的类型声明替换为这个别名:
- TypeScript全局变量声明:
declare const yourGlobalVar: GlobalUnionType;- JavaScript项目用JSDoc标注:
/** @type {GlobalUnionType} */ var yourGlobalVar;
这样既满足SonarLint的规则要求,又能让全局变量复用统一的类型别名,避免重复编写联合类型。
内容的提问来源于stack exchange,提问作者RockyCott
相关产品推荐
相关产品推荐

