amayer42
amayer42

Reputation: 83

Best way to make @types packages visible in an nx workspace

Background

I'm trying to remove resize-observer-polyfill from an nx workspace that I'm working on because it's natively supported in the browsers that we are targeting. Once I removed the polyfill, I needed to add @types/resize-observer-browser because the workspace currently uses [email protected] and my understanding is that TypeScript does not have a "native" type for ResizeObserver until v4.2 which I'd love to update to, but can't atm.

Problem

In order to make TypeScript happy, it seems like I have to go in and manually add "resize-observer-browser" to individual tsconfig compilerOptions.types entries. This didn't seem that bad to me at first. I just updated the tsconfig.lib.json file of the libraries that happened to utilize ResizeObserver. However, I soon realized I needed to also add it to the tsconfig.spec.json of the libraries so that the unit tests could run, and then I also needed to add it to the tsconfig.app.json of any applications that happened to import those libraries.

Question

Is there an easier way in an nx workspace to handle this sort of problem?

I think that I could remove the default types overrides in each of the tsconfig files, since that would let TypeScript just utilize everything that exists under node_modules/@types when compiling. I didn't want to take that path since I assume there is a good reason for the default nx library/app generators to add the types override (I assume it's to force you to be explicit and not accidentally get away with accidental imports of test code from business logic).

Upvotes: 3

Views: 1571

Answers (1)

Joshua Neuenhoff
Joshua Neuenhoff

Reputation: 58

The docs seem to recommend against this for @types packages, but /// <reference types="..." /> (e.g. /// <reference types="resize-observer-browser" />) can be also be used to include types, and might be easier to manage if the type is only used in a few places.

Docs: https://www.typescriptlang.org/docs/handbook/triple-slash-directives.html#-reference-types-

Upvotes: 2

Related Questions