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?
d.tsfiles are generated by the typescript compiler from your.tsfiles. You wouldn't import ad.tsfile, these are used by the compiler to tell it what types apply to whatever module you'veimported.