Cannot read properties of undefined (reading: xxxxx)

近日在前端开发过程中遇到了以下报错

Cannot read properties of undefined (reading: xxxxx)

在网上搜索一些解决思路,很多都说是这个属性未定义或者名称写错了,检查后发现不是。后来调试发现,是由于两个ts文件交叉引用了,于是我做了最小错误复现:

新建了两个文件a.ts与b.ts

  • a.ts
    import { b } from './b'
    export enum test {
    	j
    }
    console.log(b)
    
  • b,ts
    import { test } from './a'
    export const b = test.j
    

随后构建运行a.ts,成功复现错误: uncaught TypeError: Cannot read properties of undefined (reading 'j')