2017-07-06 98 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