I am attempting to include typings for Apple's MapKit JS in my Angular 9 application, because there are no high quality typings provided by the library, nor any good third-party typings in the @types
scoped package. However, Angular CLI isn't happy with how I've included my typings. The exact error I receive on compile is:
Module not found: Error: Can't resolve 'mapkit' in `components/map.component.ts`.
What am I doing wrong?
This is where the typings are declared, and how they are declared.
declare module 'mapkit' {
// interfaces, classes, etc here
}
{
"compileOnSave": false,
"compilerOptions": {
"baseUrl": "./",
"outDir": "./dist/out-tsc",
"sourceMap": true,
"declaration": false,
"downlevelIteration": true,
"experimentalDecorators": true,
"module": "esnext",
"moduleResolution": "node",
"importHelpers": true,
"target": "es2015",
"typeRoots": [
"node_modules/@types"
],
"lib": [
"es2019",
"dom"
]
},
"angularCompilerOptions": {
"fullTemplateTypeCheck": true,
"strictInjectionParameters": true
}
}
{
"extends": "./tsconfig.json",
"compilerOptions": {
"outDir": "./out-tsc/app",
"types": []
},
"files": [
"src/main.ts",
"src/polyfills.ts"
],
"include": [
"src/**/*.d.ts"
],
"exclude": [
"src/test.ts",
"src/**/*.spec.ts"
]
}
Here is how mapkit
is used.
import * as mapkit from 'mapkit';
The node modules folder is the default folder where all 3rd-party libraries are installed, according to which our application runs. This is used for development purposes. When we run or build our project then only required libraries are put together in a bundle and then deploy into the server.
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.
Today, we're going to figure out what are the ways to install and use third-party libraries in an Angular CLI project. Using ES6 Modules. The easiest way to add third-party libraries is to use ES6 Modules. A library, which is exported declaratively, could be loaded in a few straightforward steps.
The content is likely still applicable for all Angular 2 + versions. The Angular CLI is a command line interface tool that allows us to quickly build and run our Angular applications. The CLI can help us quickly scaffold components and services in our app while following the best practices out of the box.
One of the snag points I ran into was bringing in third party modules. The vast majority of common npm modules already have high quality typings files which you can manage through typings, but inevitably you'll run into that necessary little package that doesn't have the typings you need. Our project needed one such package, googleapis.
The CLI currently uses Webpack under the hood to handle module bundling in our apps. Webpack is a great open source tool that allows adding third part ES2015 modules easily to JavaScript applications. So lets start with adding a popular library, Lodash.
Long story short: You need to rename your folder with types and add them to the type roots.
Step #1
In your folder with types (./types) create new folder "apple-mapkit-js"
Step #2
Add your custom index.d.ts there
Step #3 Point TSC to your custom types. Update tsconfig.app.json with new typeRoots:
{
"extends": "./tsconfig.json",
"compilerOptions": {
"outDir": "./out-tsc/app",
"typeRoots" : ["./src/types/", "node_modules/@types"]
},
"files": [
"src/main.ts",
"src/polyfills.ts"
],
"include": [
"src/**/*.d.ts"
],
}
Step #4 Update your import to the
import * as mapkit from 'apple-mapkit-js'
Now it works.
But what was the issue?
Actually, according to the TypeScript documentation, if you specify typeroots, all other resources become "invisible" for the typescript, so tsc just didn't see your declarations
P.S. Checked on the Angular 9 app.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With