Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular 7 Unable to include a custom typescript definition file

I created a custom typescript definition file (brat.d.ts)

export declare class head {
   ready(callback: () => any);
}

export declare class Util {
   static embed(divId: string, collData: any, docData: any, webFontsURLs: Array<string>);
}

I am importing the above definition file in Angular 7 components like this

import {head, Util} from "../../brat/brat";

When I do ng serve, I get following error

'ERROR in ./src/app/folder-mgmt/list-documents/list-documents.component.ts Module not found: Error: Can't resolve '../../brat/brat' in '\src\app\folder-mgmt\list-documents'`

Can anyone please suggest what am i doing wrong

like image 899
Amit Dube Avatar asked Dec 03 '18 07:12

Amit Dube


People also ask

What is D TS file in angular?

The "d. ts" file is used to provide typescript type information about an API that's written in JavaScript. The idea is that you're using something like jQuery or underscore, an existing javascript library.

Is angular JavaScript or TypeScript?

Angular is a modern framework built entirely in TypeScript, and as a result, using TypeScript with Angular provides a seamless experience. The Angular documentation not only supports TypeScript as a first-class citizen, but uses it as its primary language.


1 Answers

You add declarations in a .d.ts file, and they become globally available in your project. But you'll need to tweak the tsconfig.json firstly, by adding the following:

"files": [
  "additional.d.ts"
]

Now create a ./addition.d.ts file (relative to your TypeScript project root) and add your typings there. Do not export symbols, only declare the types.

declare class head {
   ready(callback: () => any);
}

declare class Util {
   static embed(divId: string, collData: any, docData: any, webFontsURLs: Array<string>);
}

You'll now have them available globally.

If you don't want them available globally but you're instead adding typings for a module which is missing them, declare them inside a module.

declare 'module-name' {
  // ...
}

Now TypeScript will recognize those when you do import { ... } from 'module-name'.

like image 78
Lazar Ljubenović Avatar answered Oct 19 '22 07:10

Lazar Ljubenović