2017-02-10 211 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