我在打字稿中编写了一个库,并且有一个node_modules依赖项没有相应的@types定义,所以我自己写了。
我将它放在一个typings文件夹中并在tsconfig中映射,所以所有引用都在我的库中工作。当我编译它时(在tsconfig中使用declaration = true),无法找到定义,并且从另一个项目导入时它会中断。
如何以我的可分发中的声明正确的方式导出我的自定义node_modules定义?
我的tsconfig.json
{
"compilerOptions": {
"baseUrl": "./src",
"paths": {
"*": [
"./typings/*"
]
},
"outDir": "dist",
"module": "commonjs",
"declaration": true,
"declarationDir": "dist/typings",
"target": "es5",
"sourceMap": true,
"inlineSourceMap": false,
"inlineSources": false,
"experimentalDecorators": true,
"strict": true,
"noUnusedParameters": true,
"noUnusedLocals": true,
"jsx": "react",
"moduleResolution": "node",
"pretty": true,
"noEmitHelpers": true,
"importHelpers": true,
"lib": [
"dom",
"es2015"
],
"typeRoots": [
"typings"
]
},
"compileOnSave": true,
"include": [
"./src"
]
}
在我项目根目录中的typings文件夹中,我有
- typings
- rc-slider
- index.d.ts
index.d.ts的内容:
declare module "rc-slider" {
import * as React from "react";
export interface Marks {
...
export default class Slider extends React.Component<SliderProps> {}
export class Range extends React.Component<RangeProps> {}
export class Handle extends React.Component<HandleProps> {}
}
在使用rc-slider模块的文件中,我有:
import RcSlider, {
Range,
Handle,
SliderProps as RcSliderProps,
} from "rc-slider";
export interface SliderProps extends RcSliderProps {
...
export class Slider extends Component<SliderProps> {
public render() {
const { label, name, formState, ...sliderProps } = this.props;
const hasError = formState.getError(this.props.name);
let err;
if (hasError) {
err = hasError.detail || hasError.title;
}
const disabled =
formState.disabled || formState.submitting || sliderProps.disabled;
renderCss();
return (
<FormControl label={label} error={err}>
<RcSlider {...sliderProps} disabled={disabled} />
</FormControl>
);
}
}
这是一个在它上面组成的Slider,它使用我写的rc-slider定义中定义的相同属性。当我导出lib时, Slider.d.ts文件包含:
import { Component } from "react";
import { Range, Handle, SliderProps as RcSliderProps } from "rc-slider";
import { FormState } from "./FormState";
export { Range, Handle, RcSliderProps };
export interface SliderProps extends RcSliderProps {
label: string;
disabled?: boolean;
name: string;
formState: FormState;
onChange: (v: string) => void;
}
export declare class Slider extends Component<SliderProps> {
render(): JSX.Element;
}
但是rc-slider是类型&#39;任何&#39;,无处可寻,当我导入库时,这些道具不可用。如何获取打字稿以输出我的定义?
谢谢!