背景:我正在编写一个反应库(react-xarrows.tsx)-当用户为我的组件提供错误的属性时,会抛出(我写的)错误-但它显示了我在'react-arrows'中编写的行。 tsx'。
用户将看到错误
/src/Xarrow.tsx:184:12
if (!("current" in props.start))
throw Error(
`'start' property is not of type reference.
maybe you set 'start' to other object and not to React reference?.\n`
);
我想向用户指出他到底在哪里错了-我可以通过为Error提供'filename'来实现-Error([message [,fileName [,lineNumber]]])-导入的文件的文件名当前模块,即使用我的lib的用户文件-让我们将该文件称为“ /src/UsersComponent.tsx”。 (在“名称”上,我当然是指相对路径)
现在,我需要弄清楚如何获取此文件名(以及以后希望的行号;))
(我确实尝试从'module.parents'对象获得它作为对其他类似问题的答案-但模块对象没有任何父属性)