Javascript 如何使用由多个ts文件生成的单个.d.ts文件
我使用TypeScript编写了一个React库,并希望从另一个项目中使用它。使用dts生成器,我为这个库(项目)的多个.ts文件生成了一个.d.ts文件。此项目下的ts文件层次结构:Javascript 如何使用由多个ts文件生成的单个.d.ts文件,javascript,reactjs,typescript,Javascript,Reactjs,Typescript,我使用TypeScript编写了一个React库,并希望从另一个项目中使用它。使用dts生成器,我为这个库(项目)的多个.ts文件生成了一个.d.ts文件。此项目下的ts文件层次结构: Project - src - actions Action.ts ActionTypes.ts - components // Some ts files - containers // Some ts files - reducers Reduce
Project
- src
- actions
Action.ts
ActionTypes.ts
- components
// Some ts files
- containers
// Some ts files
- reducers
Reducers.ts
执行命令dtsgenerator--name TurIDM--project后,生成的.d.ts文件如下所示--在项目文件夹下输出public/turIDM.d.ts
declare module 'TurIDM/actions/ActionTypes' {
export const ACTIVE_MENU_CHANGE = "ACTIVE_MENU_CHANGE";
export const AUTHENTICATE = "AUTHENTICATE";
}
declare module 'TurIDM/reducers/Reducers' {
const reducers: ReduxActions.Reducer<StoreState, any>;
export default reducers;
}
// Rest of the module declarations...
index.jsx:
import * as React from "react";
import * as ReactDOM from "react-dom";
import { createStore, applyMiddleware } from "redux";
import { Provider } from "react-redux";
import { logger } from "redux-logger";
import {Route, Switch} from "react-router";
import { ConnectedRouter, routerMiddleware } from "react-router-redux";
import createHistory from "history/createBrowserHistory"
import reducers from "TurIDM/reducers/Reducers";
import Main from "TurIDM/containers/Main";
import Login from "TurIDM/containers/LoginContainer";
import {LOGIN_PATH, MAIN_PATH} from "TurIDM/util/Constants";
const history = createHistory();
const middleware = routerMiddleware(history);
const store = createStore(reducers, applyMiddleware(middleware));
ReactDOM.render(
<Provider store={store}>
<ConnectedRouter history={history}>
<Switch>
<Route path={LOGIN_PATH} component={Login}/>
<Route path={MAIN_PATH} component={Main}/>
</Switch>
</ConnectedRouter>
</Provider>,
document.getElementById("main")
);
但这并不是我想要的。我想为我的库生成一个.d.ts和一个.js文件,供外部项目使用。可能吗?如果是,怎么做?:)
tsconfig.json:
{
"compilerOptions": {
"outDir": "./public",
"sourceMap": true,
"noImplicitAny": false,
"experimentalDecorators": true,
"module": "commonjs",
"target": "es5",
"jsx": "react",
"lib": [
"es6",
"es7",
"dom"
],
"declaration": true,
"exclude": [
"node_modules",
"public"
]
}
}
两件事:
我将使用编译器本身生成打字,不需要外部生成器。您可以通过将声明
属性添加到tsconfig.json
中来完成此操作,请尝试将入口点添加为“files”参数:
{
"files": [
"./path/to/entry/file"
]
"compilerOptions": {
"declaration": true
}
}
要为使用者指定键入的位置,需要扩展库的package.json
{
"typings": "./path/to/the/generated/index.d.ts"
}
这样,当在其他项目中使用库时,类型应该可以正常工作。首先,感谢您的回复。我的tsconfig.json中已经有声明:true,但生成的index.d.ts文件为空。基本上,我的问题是生成一个.d.ts文件。配置中没有
“files”
键。请尝试将入口点添加为“files”
参数,它将使d.ts生成工作正常
{
"compilerOptions": {
"outDir": "./public",
"sourceMap": true,
"noImplicitAny": false,
"experimentalDecorators": true,
"module": "commonjs",
"target": "es5",
"jsx": "react",
"lib": [
"es6",
"es7",
"dom"
],
"declaration": true,
"exclude": [
"node_modules",
"public"
]
}
}
{
"files": [
"./path/to/entry/file"
]
"compilerOptions": {
"declaration": true
}
}
{
"typings": "./path/to/the/generated/index.d.ts"
}