问题
I have been trying to figure this out for the last 3-4 days, googling and reading a lot, but I don't see any example that contains my use case. I want to npm publish a library that contains its types definitions.
I've just really started to do TS because another teams need my library to support typings.
So let me try to put as much (and as little) detail as I think:
tsconfig.json:
{
"compilerOptions": {
"allowSyntheticDefaultImports": true,
"allowUmdGlobalAccess": true,
"baseUrl": ".",
"declaration": true,
"declarationMap": true,
"forceConsistentCasingInFileNames": true,
"jsx": "react",
"module": "commonjs",
"noImplicitAny": true,
"noUnusedParameters": true,
"noUnusedLocals": true,
"outDir": "./dist/",
"paths": {
},
"sourceMap": true,
"strict": true,
"target": "es6",
},
"include": [
"./src"
]
}
package.json:
"main": "dist/index.js",
"scripts": {
"tsc": "tsc",
"prepack": "npm run clean && npm run tsc",
},
"types": "./dist/index.d.ts",
src/index.ts (that is build into dist/index.js + dist/index.d.ts):
export { IAction, IState } from './types';
src/types (that is build into dist/types.js + dist/types.d.ts):
import { Map } from 'immutable';
export interface IAction {
type: string;
payload: object;
}
export interface IState extends Map<string, Map<string, any>> {
}
I have other code within this repo that use them without issue. tsc doesn't complain and builds them.
For now, I do npm pack and npm install ../path/to/my/file-0.0.1.tgz in my other project. Then when I want to use my interfaces (in this case, a redux reducer):
import { IAction, IState } from 'my-lib'; // <-- match my package.json name
const reducer = (state: IState, action: IAction) => {
...
}
I get the following errors:
error TS2709: Cannot use namespace 'IState' as a type.
error TS2709: Cannot use namespace 'IAction' as a type.
I really cannot figure out why this is happening. Is there another step I need to do in order to build my definition file? Ideally, I would prefer not to have to build it by hand.
Let me know if I need to provide more details.
Thanks for your help and patience reading this.
来源:https://stackoverflow.com/questions/59742688/how-npm-publish-typescript-interface-definition