Reputation: 17762
I want to create a function which transform a Promise based function, in my example from mongodb node client, into an Observable.
The code is very simple
export function updateOneObs(filter: Object, object: Object, collection: Collection<any>) {
return Observable.fromPromise(collection.updateOne(filter, object));
}
If I put this in VSCode, I get an error message that says
"Return type of exported function has or is using name 'UpdateWriteOpResult' from external module ".../npm-packages/observable-mongo/node_modules/@types/mongodb/index" but cannot be named."
To fix this I need to explicitly import the missing type via
import {UpdateWriteOpResult} from 'mongodb';
Considering that the error message clearly suggests that Typescript knows where to look for the missing type, is there a way to have resolved via inference without an explicit import?
In my case the import has ad additional small disadvantage: since I have set up the Typescript compiler with "noUnusedLocals": true
, once I import the missing type I have to use it somewhere if I want to avoid another error.
Upvotes: 1
Views: 598
Reputation: 250156
This error only occurs if you have "declaration": true,
. If you don't need declarations you can disable this option. If you do need declarations then you must create local names for the type. To generate declaration typescript will need to use a local name for the return type. The compiler knows where the original type lives (as it points it out in the error), but to generate the declaration it would have to give the type an alias within the module which it will not do by design (the compiler is not in the business giving names to types, especially when there may be conflicts with other names in your file)
Upvotes: 1