2017-02-10 128 views
1

我一直在尝试为'react-dates'npm模块编写自定义声明文件,但无法让编译器解析我的声明文件到该模块。找不到声明文件与'react-dates'模块的自定义声明文件

当我做import {DateRangePicker} from 'react-dates'我得到以下错误:

Could not find a declaration file for module 'react-dates'. 'absolute_path/src/node_modules/react-dates/index.js' implicitly has an 'any' type.

我的声明文件所在的路径“@类型/反应-日期/ index.d.ts”,看起来像这样:

import * as React from 'react'; 
declare class DateRangePicker extends React.Component<{}, {}> { } 

的tsconfig.json看起来是这样的:

{ 
    "compilerOptions": { 
    "outDir": "./dist/", 
    "sourceMap": true, 
    "noImplicitAny": true, 
    "strictNullChecks": true, 
    "module": "commonjs", 
    "target": "es6", 
    "jsx": "react", 
    "typeRoots": [ 
     "./@types" 
    ] 
    }, 
    "include": [ 
    "./app/**/*", 
    "./@types/**/*" 
    ] 
} 
+0

你与分型themselve取得任何进展? –

+1

我只使用图书馆的一小部分,但这是我到目前为止:https://gist.github.com/torryt/ccdaf6daf0d7df6252ac2a4539a00520 – torryt

回答

2

通过在模块声明包装它解决了它像这样:

declare module 'react-dates' { 
    import { Component } from 'react'; 
    export class DateRangePicker extends Component<{}, {}> { } 
} 

注意,导入语句必须是模块块内,否则将返回:

Invalid module name in augmentation. Module 'react-dates' resolves to an untyped module at 'path/node_modules/react-dates/index.js', which cannot be augmented.

+0

你是男人!我一直在寻找这个答案几个小时。 – Matt