In my custom TypeScript declaration file, how can I include another declaration?

Viewed 124

I have a few declarations in src/types/*.d.ts and in my src/types/role.d.ts, I have:

declare interface Role {
  id: string;
  title: string;
  openings: string;
  jobDescriptionUrl?: string;
  minCompRange: number | string;
  maxCompRange: number | string;
  location: string;
  postCovidLocation: string;
  urgency: ROLE_URGENCY;
  equity: ROLE_EQUITY;
  company?: Company;
  color: string;
  status: ROLE_STATUS;
  deletedAt?: Date;
  hiredAt?: Date;
  createdAt: Date;
  pausedAt?: Date;
  managerEmail?: string;
  managerName?: string;
  isExcRole?: boolean;
  recruiter?: Recruiter;
  offer?: File;
}

Somehow, it finds the Recruiter and File, but Company, which is declared in src/types/company.d.ts:

declare interface Company {
  companyName: string;
  companyUrl: string;
}

doesn't get found. I get an error:

src/types/role.d.ts:14:12 - error TS2304: Cannot find name 'Company'.

14  company?: Company;
              ~~~~~~~

In my tsconfig.json, I have:

    "include": [
        "src/**/*.ts",
    ]

What am I doing wrong?

2 Answers

try export interface Company instead declare interface Company maybe solved!

A special comment tells the typescript compiler to include another type-definition file

/// <reference path="myCustomDecls.d.ts" />

This should be used only in other .d.ts-files

For the typescript compiler to actually acknowledge your .d.ts-files you'll also have to specify them in the "compilerOptions" -> "typeroots" like this

"compilerOptions": {
  "typeRoots": ["./types/"] // directory containing .d.ts-files
}

If you have multiple custom declaration files, I'd recommend creating an index.d.ts file, that includes all your custom .d.ts-files using the first mentioned "special comment"

Related