TypeScript类型定义引发WebStorm重构异常的问题排查
问题:WebStorm重构.mjs文件中group函数成员时出现异常关联
库的类型定义(index.d.ts)
export function group<T extends object>(exports: T): { // it's a function (P?: {}) : {}; } & T // with the `exports` object from arguments copied onto it
业务代码示例
const A = group({ fn: () => {} }) const B = group({ fn: () => {} }) // initializing const aInstance = A() const bInstance = B() // running commands A.fn(aInstance) B.fn(bInstance)
问题现象
尝试将A.fn重构为A.refactoredFnName时,WebStorm会同时提议重构B.fn的引用,无法确定是类型定义有误、WebStorm存在bug,还是该场景下重构本身存在限制。
测试对比结果
- 在TypeScript临时文件中编写完整的group函数实现(如下),重构可正常触发,不会关联无关的
B.fn:
function group<T>(exports: T): T & {() : {}} { const fn = () => {} Object.assign(fn, exports) return fn } const A = group({ fn: (a) => {} }) const B = group({ fn: (a) => {} }) // initializing const aInstance = A() const bInstance = B() // running commands A.fn(aInstance) B.fn(bInstance)
- 将上述代码复制到.mjs文件并导入库的group函数(WebStorm已识别d.ts定义),重构时会出现异常关联;将文件重命名为.ts后,重构恢复正常。
疑问
想了解为何文件类型(.mjs vs .ts)会影响WebStorm的重构结果。
内容的提问来源于stack exchange,提问作者Anton
相关产品推荐
相关产品推荐

