在多个文件中已经有很多关于打字稿的问题..例如,这个,
打字稿导入/导出 https://stackoverflow.com/questions/72692423/typescript-import-export
有趣的问题和答案,我简化并测试了它,见下文..但无论我尝试什么,我仍然得到
未捕获的类型错误:无法读取未定义的属性(读取“A”)
.. 一样任何其他示例我在网上找到了 TypeScript 中的导入/导出。无论我做什么,无论我尝试使用或不使用模块导出(类、函数、const)对象:我都会得到相同的错误。
也许我的 NPM/TSC/React 配置有问题?我应该改变例如tsconfig.js 当我想在项目中使用多个打字稿文件时?我迷失了,我错过了什么?
tsconfig.json
{ // TypeScript configuration file: provides options to the TypeScript
// compiler (tsc) and makes VSCode recognize this folder as a TS project,
// enabling the VSCode build tasks "tsc: build" and "tsc: watch".
"compilerOptions": {
"target": "es5", // Compatible with older browsers
"module": "umd", // Compatible with both Node.js and browser
"moduleResolution": "node", // Tell tsc to look in node_modules for modules
"sourceMap": true, // Creates *.js.map files
"jsx": "react", // Causes inline XML (JSX code) to be expanded
"strict": true, // Strict types, eg. prohibits `var x=0; x=null`
"alwaysStrict": true // Enable JavaScript's "use strict" mode
},
"include": ["**/*.ts", "**/*.tsx"],
"exclude": ["node_modules"]
}
首先.tsx
const A ={
val: 'A'
}
export { A }
app.tsx
import { A } from "./first";
// ... other code
function reportPerson()
{
console.log(A);
}
.. 两个文件都使用 TSC 转换为 .js,但 Google Chrome 控制台将 A 报告为未定义,
两个 tsx 文件都在同一目录中,TSC 将它们都转换为 JS,没有任何问题。
这是怎么回事 ?