【发布时间】:2019-03-27 23:21:58
【问题描述】:
我目前正在学习 TypeScript 并尝试定义自己的类型。我目前正在使用 .d.ts 文件来声明我的类型。
我创建了一个文件./src/typings/git-types.d.ts:
declare module "git-types" {
export interface GitRepoListItem {
repoName: string;
repoPath: string;
}
export interface GitReturnObject {
value: any;
errorCode: GitErrorCode;
}
export enum GitErrorCode {
UnknownError = 1,
GitNotFound = 2,
NoValidPathGiven = 3,
LocalChangesPreventCheckout = 4,
LocalChangesPreventPull = 5
}
export interface GitCommit {
hash: string;
author: string;
commitDate: string;
commitTime: string;
commitMessage: string;
}
}
现在我尝试导入这个模块,以便在另一个文件 CommitHistory.ts 中使用我的类型:
import { GitCommit } from "git-types";
class CommitHistory {
commitList: GitCommit[] = [];
...
}
但是当我尝试运行我的代码时,编译器失败并给我以下错误:
Module not found: Can't resolve 'git-types' in './src/CommitHistory.ts
正如你在我的tsconfig.json-file 中看到的,整个“src”文件夹都包含在内:
{
"compilerOptions": {
"target": "es6",
"lib": ["dom", "dom.iterable", "esnext"],
"allowJs": true,
"skipLibCheck": true,
"esModuleInterop": true,
"allowSyntheticDefaultImports": true,
"strict": true,
"forceConsistentCasingInFileNames": true,
"module": "esnext",
"moduleResolution": "node",
"resolveJsonModule": true,
"isolatedModules": true,
"noEmit": true,
"jsx": "preserve",
"experimentalDecorators": true
},
"include": ["src"]
}
如何声明模块才能使用我的类型?
【问题讨论】:
-
d.ts文件由 typescript 编译器从您的.ts文件生成。您不会导入d.ts文件,编译器使用这些文件来告诉它哪些类型适用于您imported 的任何模块。
标签: javascript reactjs typescript