首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >从Object.assign返回和类型记录中的自定义对象有什么不同?

从Object.assign返回和类型记录中的自定义对象有什么不同?
EN

Stack Overflow用户
提问于 2021-05-10 06:42:22
回答 2查看 404关注 0票数 1

我在NestJS中使用typeorm,在app.module.ts中我尝试通过应用forRootAsync来初始化数据库设置

代码语言:javascript
运行
复制
TypeOrmModule.forRootAsync({
    imports: [ConfigModule],
    inject: [ConfigService],
    useFactory: async (configService: ConfigService) => ({
        type: configService.get('database.type'),
        host: configService.get('database.host'),
        port: configService.get('database.port'),
        username: configService.get('database.username'),
        password: configService.get('database.password'),
        database: configService.get('database.database'),
        entities: configService.get('database.entities'),
        synchronize: configService.get('database.synchronize'),
        logging: configService.get('database.logging'),
    }),
}),

在编译过程中,我得到了以下错误:

代码语言:javascript
运行
复制
error TS2322: Type '(configService: ConfigService) => Promise<{ type: "mysql" | "mariadb" | "postgres" | "cockroachdb" | "sqlite" | "mssql" | "sap" | "oracle" | "cordova" | "nativescript" | "react-native" | "sqljs" | "mongodb" | "aurora-data-api" | "aurora-data-api-pg" | "expo" | "better-sqlite3"; ... 7 more ...; logging: LoggerOptions...' is not assignable to type '(...args: any[]) => TypeOrmModuleOptions | Promise<TypeOrmModuleOptions>'.
  Type 'Promise<{ type: "mysql" | "mariadb" | "postgres" | "cockroachdb" | "sqlite" | "mssql" | "sap" | "oracle" | "cordova" | "nativescript" | "react-native" | "sqljs" | "mongodb" | "aurora-data-api" | "aurora-data-api-pg" | "expo" | "better-sqlite3"; ... 7 more ...; logging: LoggerOptions; }>' is not assignable to type 'TypeOrmModuleOptions | Promise<TypeOrmModuleOptions>'.
    Type 'Promise<{ type: "mysql" | "mariadb" | "postgres" | "cockroachdb" | "sqlite" | "mssql" | "sap" | "oracle" | "cordova" | "nativescript" | "react-native" | "sqljs" | "mongodb" | "aurora-data-api" | "aurora-data-api-pg" | "expo" | "better-sqlite3"; ... 7 more ...; logging: LoggerOptions; }>' is not assignable to type 'Promise<TypeOrmModuleOptions>'.
      Type '{ type: "mysql" | "mariadb" | "postgres" | "cockroachdb" | "sqlite" | "mssql" | "sap" | "oracle" | "cordova" | "nativescript" | "react-native" | "sqljs" | "mongodb" | "aurora-data-api" | "aurora-data-api-pg" | "expo" | "better-sqlite3"; ... 7 more ...; logging: LoggerOptions; }' is not assignable to type 'TypeOrmModuleOptions'.
        Type '{ type: "mysql" | "mariadb" | "postgres" | "cockroachdb" | "sqlite" | "mssql" | "sap" | "oracle" | "cordova" | "nativescript" | "react-native" | "sqljs" | "mongodb" | "aurora-data-api" | "aurora-data-api-pg" | "expo" | "better-sqlite3"; ... 7 more ...; logging: LoggerOptions; }' is not assignable to type '{ retryAttempts?: number; retryDelay?: number; toRetry?: (err: any) => boolean; autoLoadEntities?: boolean; keepConnectionAlive?: boolean; verboseRetryLog?: boolean; } & Partial<AuroraDataApiConnectionOptions>'.
          Type '{ type: "mysql" | "mariadb" | "postgres" | "cockroachdb" | "sqlite" | "mssql" | "sap" | "oracle" | "cordova" | "nativescript" | "react-native" | "sqljs" | "mongodb" | "aurora-data-api" | "aurora-data-api-pg" | "expo" | "better-sqlite3"; ... 7 more ...; logging: LoggerOptions; }' is not assignable to type 'Partial<AuroraDataApiConnectionOptions>'.
            Types of property 'type' are incompatible.
              Type '"mysql" | "mariadb" | "postgres" | "cockroachdb" | "sqlite" | "mssql" | "sap" | "oracle" | "cordova" | "nativescript" | "react-native" | "sqljs" | "mongodb" | "aurora-data-api" | "aurora-data-api-pg" | "expo" | "better-sqlite3"' is not assignable to type '"aurora-data-api"'.
                Type '"mysql"' is not assignable to type '"aurora-data-api"'.

19             useFactory: async (configService: ConfigService) => ({
               ~~~~~~~~~~

  node_modules/@nestjs/typeorm/dist/interfaces/typeorm-options.interface.d.ts:19:5
    19     useFactory?: (...args: any[]) => Promise<TypeOrmModuleOptions> | TypeOrmModuleOptions;
           ~~~~~~~~~~
    The expected type comes from property 'useFactory' which is declared here on type 'TypeOrmModuleAsyncOptions'

但是,如果在返回之前应用Object.assign(),它将通过

代码语言:javascript
运行
复制
TypeOrmModule.forRootAsync({
    imports: [ConfigModule],
    inject: [ConfigService],
    useFactory: async (configService: ConfigService) =>
        Object.assign({
            type: configService.get('database.type'),
            host: configService.get('database.host'),
            port: configService.get('database.port'),
            username: configService.get('database.username'),
            password: configService.get('database.password'),
            database: configService.get('database.database'),
            entities: configService.get('database.entities'),
            synchronize: configService.get('database.synchronize'),
            logging: configService.get('database.logging'),
        }),
}),

这是因为Object.assign()将对象转换为任何类型吗?异步初始化类型的通常方法是什么?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2021-05-10 07:33:31

在搜索之后我找到了这条线。

https://github.com/nestjs/nest/issues/1119

它说我们需要特别指定类型。

因此,我在返回对象中更改了type属性:

代码语言:javascript
运行
复制
type: configService.get('database.type') as 'mysql',

但是如果删除Object.assign(),我仍然会得到相同的编译错误,并且我注意到

代码语言:javascript
运行
复制
password: configService.get('database.password')

将configService.get()转换为

代码语言:javascript
运行
复制
string | (() => string) | (() => Promise<string>);

这会导致问题,所以我需要传递类型,最后的配置如下所示:

代码语言:javascript
运行
复制
TypeOrmModule.forRootAsync({
    imports: [ConfigModule],
    inject: [ConfigService],
    useFactory: async (configService: ConfigService) => ({
        type: configService.get('database.type') as 'mysql',
        host: configService.get('database.host'),
        port: configService.get('database.port'),
        username: configService.get('database.username'),
        password: configService.get('database.password'),
        database: configService.get<string>('database.database'),
        entities: [configService.get('database.entities')],
        synchronize: configService.get<boolean>('database.synchronize'),
        logging: configService.get<boolean>('database.logging'),
    }),
})

但我还是不知道为什么它会把密码转换成那种奇怪的类型。

票数 0
EN

Stack Overflow用户

发布于 2021-05-10 07:19:34

正如我从你的日志- Type '"mysql"' is not assignable to type '"aurora-data-api"'.中看到的

你可以参考- 覆盖在ormconfig中定义的选项.在这里,它显式地提到了一个示例来附加自定义命名策略或自定义记录器。

TypeORM提供了getConnectionOptions函数,该函数从ormconfig文件或环境变量中读取连接选项。- 关于TypeORM集成的提示

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/67465876

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档