2017-07-06 72 views
0

說實話,我很少知道實際發生了什麼。我試圖創建一個基於here顯示的聲明文件 - 這是我以前從未做過的。ES6風格的輸入從打字稿模塊定義(module.d.ts)

我有這些文件(重命名和編輯因工作規則):

project_one

some_interface.ts

import SomeOtherInterface from "some_other_file"; 

interface ThisInterface 
{ 
    aMethodRequiring(aThing: SomeOtherInterface): void; 
} 

export default ThisInterface; 

index.d.ts

export { default as ThisInterface } from "some_interface"; 

tsconfig.json

{ 
    "compilerOptions": { 
    "baseUrl": "src", 
    "noImplicitAny": true, 
    "target": "es2015" 
    }, 
    "exclude": [ 
    "src/**/*.type.ts" 
    ], 
    "include": [ 
    "src/**/*.ts" 
    ] 
} 

project_two

some_class.ts

/// <reference types="project_one" /> 
    /*^This seems to be fine^*/ 

import SomethingInThisProject from "a_file_in_this_project"; 
import { ThisInterface }  from "project_one"; // <=== This is the line that fails. 

class ThatClass 
{ 
    private instanceOfThisInterface : ThisInterface; 

    constructor(saidInstance: ThisInterface) 
    { 
    this.instanceOfThisInterface = saidInstance; 
    } 

    // The useful stuff 
} 

export default ThatClass; 

tsconfig.json

{ 
    "compilerOptions": { 
    "baseUrl": "src", 
    "noImplicitAny": true, 
    "target": "es2015" 
    }, 
    "exclude": [ 
    "src/**/*.type.ts" 
    ], 
    "include": [ 
    "src/**/*.ts" 
    ] 
} 

的package.json

{ 
    "name": "project_two", 
    "version": "0.1.0", 
    "description": "A Second Project", 
    "main": "index.js", 
    "repository": "https://github.com/project/two", 
    "author": "Cyle Ven Dawson", 
    "license": "MIT", 
    "devDependencies": { 
    ... 
    }, 
    "dependencies": { 
    ... 
    "project_one": "^0.5.0" 
    } 
} 

打字稿發現project_one聲明文件就好了。然而,它大聲對我說關於它的import ing,說[ts] cannot find module 'project_one'.我想象這個代碼有更多的錯誤,而不僅僅是我如何嘗試從聲明文件中導入東西,但現在我期望讓import行工作。

我誤解了什麼?

+0

你能告訴我們package.json嗎?是否將'project_one'正確註冊爲'project_two'的依賴項? –

+0

@ E_net4添加了package.json內容。如所示,將「project_one」設置爲依賴項。 – dawsonc623

回答

0

定製打字的位置應在tsconfig.json

{ 
    "compilerOptions": { 
     "typeRoots": [ 
      "./custom_typings", 
      "./node_modules/@types" 
     ] 
    } 
} 

見引用tsconfig documentation更詳細的信息。

+0

TypeScript似乎找到了聲明就好了。在我的編輯器中,我可以使用「go to」功能跳轉到'node_modules'下'project_one'目錄下的聲明文件。錯誤不在'references'行,而是'import {ThisInterface} ...'行。 – dawsonc623

+0

爲了知道是否可以從'project_one'中導入任何內容,請執行以下命令:'import * as project_one from「project_one」'? – guwere

+0

剛剛試過 - 得到了同樣的結果'[ts]找不到模塊'project_one'.'錯誤。 – dawsonc623