[英]Imported symbol from typescript library is undefined
I am developing a React/Redux application using Typescript. 我正在使用Typescript开发一个React / Redux应用程序。 In order to simplify development, I want to create libraries for each "domain" or "feature" that will encapsulate specifics of the domain and expose an API to the top-level application.
为了简化开发,我想为每个“域”或“功能”创建库,这些库将封装域的详细信息并将API公开给顶层应用程序。
So far I have created a library (in Typescript) named @foo/foo
and a main application that depends on this library. 到目前为止,我已经创建了一个名为
@foo/foo
的库(使用Typescript)和一个依赖于该库的主应用程序。 The library has a toplevel index.ts
file that looks like: 该库具有一个顶级
index.ts
文件,如下所示:
export * from "./services/Executor";
export * from "./services/Foo";
export * from "./services/Result";
export * from "./services/listFoo";
The file ./services/Executor.ts
exports a bunch of functions like: 文件
./services/Executor.ts
导出了许多函数,例如:
export const fetchFoo = ...
Then I created an application using create-react-app --script react-ts
and in one of the actions file I do: 然后,我使用
create-react-app --script react-ts
创建了一个应用程序,并在其中一个操作文件中执行了以下操作:
import { fetchFoo } from '@foo/foo';
...
But when I run the React application using npm run start
, I got: 但是当我使用
npm run start
运行React应用npm run start
,我得到了:
TypeError: fetchFoo is not a function
TypeError:fetchFoo不是函数
From my research on the web I assume I am messing up with modules import semantics: Both packages are configured (in tsconfig.json
) to be compiled to es6
with esModuleInterop: true
. 根据我在Web上的研究,我假设我正在弄乱模块的导入语义:这两个软件包都已配置(在
tsconfig.json
),使用esModuleInterop: true
编译为es6
。 I tried changing modules
field but to no avail. 我尝试更改
modules
字段,但无济于事。
What am I doing wrong? 我究竟做错了什么?
Update : Here is some code demonstrating the problem: https://github.com/abailly/react-ts-import-module 更新 :这是一些演示问题的代码: https : //github.com/abailly/react-ts-import-module
You should reference your build output under the "main" key in your package.json and point to the main declaration file under the "types" key: 您应该在package.json的“ main”键下引用构建输出,并在“ types”键下指向主声明文件:
{
...
"main": "build/index.js",
"types": "build/index.d.ts",
...
}
CRAs tsc/webpack will look for a JS file when bundling. 捆绑时,CRA tsc / webpack将查找JS文件。 Imported modules are not transpiled by default (though this could be enabled, but since you build your lib separately this should not be necessary)
导入的模块默认情况下不会进行转译(尽管可以启用此功能,但是由于您是单独构建库的,因此这不是必需的)
I hope that works (and helps if yes ;) 我希望它能奏效(如果可以,会有所帮助;)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.