你不需要使用
declare module
. 相对路径在那个里无论如何都不起作用,它们只能在模块中使用
增强
首先在各自的文件夹中声明组件。我过去只是
React.ComponentType
,但你可以更具体一些。
./src/components/Button/index.d.ts
import * as React from 'react';
declare const Button: React.ComponentType;
export default Button;
./src/components/Text/index.d.ts
import * as React from 'react';
declare const Text: React.ComponentType;
export default Text;
桶锉
./src/index.d.ts
export { default as Text } from './components/Text'
export { default as Button } from './components/Button'
现在您的组件可以从这两个站点访问
./src
和从各自的文件夹中。如果我们创建了另一个名为
consumer
./consumer/index.ts
/**
* Import the re-exported components from `./src/index.d.ts`:
*/
import { Button } from '../src/';
/**
* Or import from their respective folders:
*/
import Text from '../src/components/Text';
请记住,以这种方式解析路径需要设置
"moduleResolution"
到
"node"
在你的
tsconfig.json
.
{
"compilerOptions": {
"baseUrl": ".",
"moduleResolution": "node"
}
}