Assuming I need to download an npm package, is it possible to override the declarations (.d.ts) of the package itself?
By creating a .d.ts file with the new types, in the main project folder I can't override the package's types. The only way to enable my types is to go and manually edit the .d.ts files of the package itself.
--- Example of the problem ---
The package I'm referring to has this declaration:
declare class FooClass<T> {
get foo(): T;
}
The declaration is very flexible on typescript projects and I can easily handle it:
const fooInstance = new FooClass<boolean>();
console.log(fooInstance.foo);
Suppose, however, that I'm using a Javascript project (and I care about types). In that case I can't specify the type of FooClass between the brackets:
const fooInstance = new FooClass();
console.log(fooInstance.foo);
Therefore I create a file foo.d.ts:
declare class FooClass {
get foo(): boolean;
}
However foo at compile time will not be considered boolean.
That depends on what declarations you are trying to 'override'. As it happens, certain types of entities in TypeScript will be automatically 'merged' if declared multiple times.
Read the documentation on declaration merging for more information.