Reputation: 122
I am currently learning TypeScript and trying to define my own types. I am currently using .d.ts files to declare my types.
I created a file ./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;
}
}
Now i try to import this module in order to use my types in another file CommitHistory.ts
:
import { GitCommit } from "git-types";
class CommitHistory {
commitList: GitCommit[] = [];
...
}
But when i try to run my code, the compiler fails and gives me the following error:
Module not found: Can't resolve 'git-types' in './src/CommitHistory.ts
As you can see in my tsconfig.json
-file the whole "src"-folder is included:
{
"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"]
}
How do I have to declare the module in order to use my types?
Upvotes: 0
Views: 1987
Reputation: 23762
In src/typings/git-types.d.ts
, it's unnecessary to write declare module "git-types"
. Just export your types:
// src/typings/git-types.d.ts
export interface GitRepoListItem {
repoName: string;
repoPath: string;
}
// …
Then, these types can be imported with a relative path:
// src/CommitHistory.ts
import { GitCommit } from "./typings/git-types";
If you installed a real npm package git-types
that contains only JavaScript code and you want to provide types yourself, then your code is fine and should work.
Upvotes: 1