DEV Community

Loïc Sapone
Loïc Sapone

Posted on Originally published at loic-sapone.fr

AssetMapper and TypeScript: getting the missing types with a Symfony bundle

AssetMapper simplifies asset management in Symfony, but it doesn't download TypeScript types. Your IDE knows nothing about the JS libraries you use, everything is an implicit any, and type errors slip through the cracks.

The problem: AssetMapper doesn't download types

With npm, when you install a dependency, you get the entire package: the sources, the TypeScript declaration files (.d.ts), the metadata. TypeScript knows where to look for types, and the IDE follows.

AssetMapper works differently. It only downloads the JS file exposed by the package, which is enough for execution, but not for type checking. The .d.ts files are not part of the downloaded bundle.

Direct consequence: TypeScript and IDEs like VSCode or PhpStorm find no type definitions for your imports. Everything is an implicit any. Type errors go undetected and autocompletion doesn't work.

How TypeScript resolves types

Before looking for a solution, you need to understand the resolution mechanism.

A JS package can ship its own type definitions through a types or typings field in its package.json. This field points to a root .d.ts file, which can itself import other declaration files. This graph of files is what TypeScript walks to resolve types at compile time.

In a standard npm project, all of that lives in node_modules. TypeScript knows to look there by default. Without node_modules, you have to tell it explicitly where to look. That's the role of the paths config in tsconfig.json.

{
  "compilerOptions": {
    "paths": {
      "@hotwired/stimulus": ["./assets/vendor/@hotwired/stimulus/dist/types/index.d.ts"]
    }
  }
}
Enter fullscreen mode Exit fullscreen mode

paths maps an import identifier (like @hotwired/stimulus) to one or more local paths. TypeScript goes through them in order until it finds a match.

The manual solution

The procedure is simple, but tedious to do by hand for every package.

First step: identify whether the package exposes types. Look at its package.json on jsDelivr or npm, and search for the types or typings field.

curl https://cdn.jsdelivr.net/npm/@hotwired/stimulus/package.json | jq '.types'
# → "dist/types/index.d.ts"
Enter fullscreen mode Exit fullscreen mode

Second step: fetch the root .d.ts file, then every imported file recursively. A declaration file can import others through relative paths: you need to download them all for resolution to work.

# Fetch the root file
curl -o assets/vendor/@hotwired/stimulus/dist/types/index.d.ts \
  https://cdn.jsdelivr.net/npm/@hotwired/stimulus/dist/types/index.d.ts

# Fetch the referenced files
curl -o assets/vendor/@hotwired/stimulus/dist/types/core.d.ts \
  https://cdn.jsdelivr.net/npm/@hotwired/stimulus/dist/types/core.d.ts
Enter fullscreen mode Exit fullscreen mode

Third step: update tsconfig.json with the matching paths entry.

This procedure works. We validated it on several packages. But it doesn't scale: every importmap:require, you start over. And if a package updates its types, you have no way of knowing.

TypescriptTypesBundle: automating all of this

To avoid repeating this procedure by hand, we built IQ2i/typescript-types-bundle. The idea is simple: read importmap.php, detect the packages that expose types, download the .d.ts files, and update tsconfig.json.

Installation goes through Composer, as a development dependency: the bundle is only IDE tooling, nothing is needed in production.

composer require --dev iq2i/typescript-types-bundle
Enter fullscreen mode Exit fullscreen mode

Once installed, a single command is enough to synchronize the types of every package declared in the importmap:

php bin/console typescript:types:download
Enter fullscreen mode Exit fullscreen mode

The bundle queries jsDelivr to fetch each package's package.json, checks for a types or typings field, downloads all the package's .d.ts files into assets/vendor/@types/, then updates tsconfig.json. The paths point to each package's folder, not to a specific file: it's the types field of the downloaded package.json that tells TypeScript the entry point. Here is what that looks like on a project with Stimulus and Tom Select:

{
  "compilerOptions": {
    "paths": {
      "@hotwired/stimulus": ["./assets/vendor/@types/@hotwired/stimulus"],
      "tom-select": ["./assets/vendor/@types/tom-select"]
    }
  }
}
Enter fullscreen mode Exit fullscreen mode

And if a package doesn't declare a types field? The bundle doesn't stop there: it looks for the matching @types/* package on DefinitelyTyped, as npm would. A package like lodash, which doesn't ship its own declarations, still gets its types through @types/lodash. If nothing exists on the DefinitelyTyped side either, the package is skipped without error.

The bundle only touches the paths section. If tsconfig.json already exists, only the missing entries are added, the rest is preserved. If it doesn't exist, it is created.

The result is visible immediately in the editor: autocompletion on imports, method signatures, type errors flagged as you type.

Give it a try!

The bundle is open source and available on GitHub: IQ2i/typescript-types-bundle. The README covers installation and usage of the command. If you come across a package whose types don't resolve correctly, issues are open: that's exactly the kind of edge case that makes the tool better.

Top comments (0)