Add custom typings files to Angular CLI

2020-03-13 04:11发布

问题:

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.

回答1:

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.