Fix typing issues locally with `paths`
The Definitely Typed community does an awesome job typing packages that ship without types, but as most community maintained efforts it is a work in progress.
If you find an error in a @types declaration you can expand it by including a declaration in the paths
entry of the compilerOptions
in your tsconfig file. If you use paths
you must also specify a baseUrl
.
For example, in @types/mongoose
the SchemeOptions
interface is incomplete: the optimisticConcurrency
flag is missing and typescript throws an error. Let’s fix it.
We’ll a declaration file index.d.ts
with the addition that we wish to do. We’ll do it in the following path: <project-root>/typings/mongoose/index.d.ts
.
declare module "mongoose" {
export interface SchemaOptions {
optimisticConcurrency?: boolean;
}
}
Then we’ll update the tsconfig, to include the new path.
// tsconfig.json
"compilerOptions: {
//...
"baseUrl": ".",
"paths": {
"*": [
"typings/*"
]
}
}
The baseUrl
is relative to the tsconig.
With the wildcard *
you are telling the compiler that for every module it should check the specified paths (independently of weder they are type declarations or executable code).
We could have used a different path, but with this structure we can add typings for any library.
Of course, this is only a short term fix and we should open a pull request to DefinitelyTyped.