0

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?

1
  • 1
    d.ts files are generated by the typescript compiler from your .ts files. You wouldn't import a d.ts file, these are used by the compiler to tell it what types apply to whatever module you've imported. Commented Mar 27, 2019 at 14:54

1 Answer 1

1

Do not declare your typings as if they came from a global module

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";

Or, declare types for an existing global modules

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.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for your help. Exporting the types directly works now for me. However as ethane already said in his comment I had to rename the git-types.d.ts file to git-type.ts.
@Psycho No. You don't have to rename. The extension .d.ts is better for files that contain only types because the compiler doesn't emit file for these files.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.