typeorm: ConnectionOptions does not allow variable assigned to type
src/db.ts(14,39): error TS2352: Type '{ type: string; host: string; port: number; username: string; password: string; database: string;...' cannot be converted to type 'ConnectionOptions'.
Type '{ type: string; host: string; port: number; username: string; password: string; database: string;...' is not comparable to type 'CordovaConnectionOptions'.
Types of property 'type' are incompatible.
Type 'string' is not comparable to type '"cordova"'.
Where I am trying to pass in the connection options. ie:
const opts: ConnectionOptions = {
type: wConfig.type,
host: wConfig.host,
...
FWIW, I am actually trying to create a Postgres connection in this case. Not sure why it chose to report cordova as the type The Postgres config isn’t directly exposed, either
my workaround:
const opts: ConnectionOptions = {
type: 'postgres',
host: wConfig.host,
...
};
(opts.type as any) = wConfig.type;
I’m not using any specific features of postgres, so any db connection as configured should be fine, and would/should not require code modification.
About this issue
- Original URL
- State: closed
- Created 7 years ago
- Comments: 15 (4 by maintainers)
Guys, just use the database you are really using, its pointless to set a type from the configuration because each type has its own specifics, you cannot practically really have it dynamic.
There are lots of use cases for wanting a variable for the database type … for tools, or testing, or for that matter apps that need to connect to more than one type of database are pretty common.
I recognize this issue isn’t exactly a show-stopper, but it got in my way too, and I had to write some ugly code to work around it.
And dismissing the request as “pointless” is offensive. 😦
This is what it worked for me in typescript import {ConnectionOptions, DatabaseType} from ‘typeorm’;
The Issue is marked as Closed but seems to be persistent
@ typeorm 0.2.15
Throws error:
where this issue happened too and the solution was add type
DataSourceOptionson return. The solved code:for some reason typeorm understand ‘mysql | postgres | sqlite…’ as custom types so if you inject string it will break
This one works for me but it’s ugly. You can add more types in switch section.
type: 'postgres' as constshould also work.yeah, I cant add it to external, you can use full path for now. Also you can do:
but it does not make sense because if you have a type stored inside a variable then it suppose to be other then postgres too