Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add custom typings files to Angular CLI

I have a SPA with Angular CLI framework and a Javascript library written by a team member. I need to inject this JS file into the app. My thought is

  1. to write a type definition for the file;
  2. import both JS file and its type definitions (.d.ts) to the app

My question is how to add the custom type definition file into my Angular CLI app? (I know the JS file can be added from "scripts": [] in .angular-cli.json file, right?)

I saw some posts for adding external js library to angular-cli, but they all seem on npm which doesn't work for me apparently.

like image 789
Viv Avatar asked Jun 29 '17 23:06

Viv


People also ask

What is Typings in Angular?

Typings describes contract of libraries you use. This allows the TypeScript compiler that what you use exist (classes, properties, ...). You can install typings from a repository using the typings command or let the compiler find out them leveraging the strategy specified in the tsconfig.

What are .TS files in Angular?

ts: This file is a unit testing file related to app component. This file is used along with other unit tests. It is run from Angular CLI by the command ng test.

Do I need to install TypeScript for Angular?

Knowledge of TypeScript is helpful, but not required. Angular requires an active LTS or maintenance LTS version of Node.js. For information about specific version requirements, see the engines key in the package.json file. For more information on installing Node.js, see nodejs.org.

Does Angular use Babel?

Since Angular utilizes decorators and class properties heavily and Babel doesn't support them in those presets, we need to add them manually. Please note that plugin-proposal-decorators needs to be additionally configured — we need to use legacy decorators mode.


1 Answers

You have a couple of options here:

  1. In the src dir for newly created applications there is a file typings.d.ts which you can use reference your team member's .d.ts file which will expose those types throughout your application.

  2. In the tsconfig.json file you can add the path to the node_modules folder where this package lives to the typeRoots array which will make those typing available too.

like image 74
Brocco Avatar answered Sep 18 '22 08:09

Brocco