How to use / declare an external module, which won't pass strict and noImplicitReturns TypeScript settings in Angular 11?

Viewed 504

We have an Angular 11 application with strict mode turned on. It has these values in the compilerOptions configurtation in tsconfig.json:

"strict": true,
"noImplicitReturns": true,

Now we want to use an external library (leaflet geoman). The library is imported like this:

import * as L from 'leaflet';
import '@geoman-io/leaflet-geoman-free';

Unfortunately it includes implicit any types as well as implicit any return types:

Parameter 'options' implicitly has an 'any' type.
'setLang', which lacks return-type annotation, implicitly has an 'any' return type

How can I tell the compiler to ignore these errors from the module during compilation but keep strict mode turned on for the rest of the project?

2 Answers

Even though the @types are included in the leaflet and leaflet-geoman-free npm packages, you should be able to use the "as" keyword to tell TypeScript that an element is respectful of a certain interface / class.

const x = fn() as IYourInterface;

If you know the structure of the return data, you can define an interface on your own. If you want, you can ignore the typescript triggered error by adding this comment before the line that's triggering the error:

// @ts-ignore
line of code that triggers the error

Obviously, if you are sure that the package is lacking some type definitions, the best way is to create an interface by yourself.

Related