0

Since upgrading to Typescript 4.4.2 (needed to support Angular 13), the require syntax is no longer supported.

Now it seems I must use this syntax instead:

import * as d3ContextMenu from 'd3-context-menu';

Type definitions don't exist for d3-context-menu in DefinitelyTyped or anywhere else that I could find. So, I've tried creating my own using these instructions: (see "Plan C" here)

The problem is when I try to use these type definitions in a project compiled using ng-packagr I can't get it to work.(it works in a regular Angular project)

What am I missing here? What's the secret sauce to including this type definition file?

Files used in an unsuccessful attempt to import the custom type definition

enter image description here

HankScorpio
  • 3,612
  • 15
  • 27
  • Have you tried to import only some specific parts of the library with desctructing? You can also try to use the type modifieren on imports. Which will only import type definitions. – DaSch Feb 21 '22 at 14:47
  • Have you tried to change the include path to common-library/d3-context-menu.d.ts? – DaSch Feb 21 '22 at 14:49
  • The library is just a single function so we can't import only part of it (if necessary we'll just copy its code to our codebase). What do you mean by "use the type modifieren on imports"? Can you provide a link with more info? Yes, we've tried that include path, and several others, with no success. – HankScorpio Feb 22 '22 at 17:40
  • Im meant this: https://www.typescriptlang.org/docs/handbook/release-notes/typescript-3-8.html but I doubt this will solve it, as you also need the library as such. Have you checked the tsconfig.lib.prod.json as this is the one used for the production build. It looks like your non prod works. – DaSch Feb 23 '22 at 18:12

1 Answers1

0

When you build an Angular library using ng-packgr it does not bundle the dependencies into the library bundle. Instead what we usually do is adding the dependency needed by the library to the peerDependencies of the library and then to the App's dependencies. That means that even if you add the declaration for d3-context-menu to your library TS won't be able to find it automagically and you need to specify the declaration file for d3-context-menu at the application level.

Shlang
  • 2,495
  • 16
  • 24