简体   繁体   English

过滤掉打字稿中接口不需要的对象属性

[英]filter out unneeded object properties for interface in typescript

As background, I'm using Prisma (graphql), mysql2 (nodejs) and typescript.作为背景,我使用 Prisma (graphql)、mysql2 (nodejs) 和 typescript。

I'm using an interactive command line script to connect to mysql2.我正在使用交互式命令行脚本连接到 mysql2。

This is the warning I am seeing:这是我看到的警告:

Ignoring invalid configuration option passed to Connection: type.忽略传递给 Connection: 类型的无效配置选项。 This is currently a warning, but in future versions of MySQL2, an error will be thrown if you pass an invalid configuration options to a Connection这是当前的警告,但在 MySQL2 的未来版本中,如果您将无效的配置选项传递给 Connection,则会引发错误

This is how I instantiate mysql2, in my MysqlConnector class:这就是我在 MysqlConnector 类中实例化 mysql2 的方式:

this.connectionPromise = await this.mysql.createConnection(this.connectionOptions)

"connectionOptions" is set in the class constructor: “connectionOptions”在类构造函数中设置:

constructor(connectionDetails: MysqlConnectionDetails) {

This is my type definition:这是我的类型定义:

export interface MysqlConnectionDetails {
  host: string
  port: number
  user: string
  password: string
  database?: string
}

This is the type definition of the object that I pass into my MysqlConnector class:这是我传递给 MysqlConnector 类的对象的类型定义:

export interface DatabaseCredentials {
  type: DatabaseType
  host: string
  port: number
  user: string
  password: string
  database?: string
  alreadyData?: boolean
  schema?: string
  ssl?: boolean
  filter?: any
}

So, I am passing in an object that has additional parameters that mysql2 does not want/need.因此,我传入了一个对象,该对象具有 mysql2 不想要/不需要的附加参数。 I am new to Typescript.我是打字稿的新手。 I tried this, before passing the object in to the MysqlConnector class:在将对象传递给 MysqlConnector 类之前,我试过这个:

let forDeletion = ['type', 'alreadyData']
connector = new MysqlConnector(credentials.filter(item => !forDeletion.includes(item)))

But I get an error, saying "filter" is not a property on the type "DatabaseCredentials", and I concluded this is probably not the right way to do this.但是我收到一个错误,说“过滤器”不是“DatabaseCredentials”类型的属性,我得出的结论是这可能不是正确的方法。

I assumed there is a way in Typescript (through tsconfig) automatically to filter out properties that are not in the receiving type.我认为 Typescript(通过 tsconfig)有一种方法可以自动过滤掉不在接收类型中的属性。 "MysqlConnectionDetails" doesn't have a property "type", in its definition, so it gets it dynamically, when I pass in another type. “MysqlConnectionDetails”在其定义中没有属性“type”,因此当我传入另一种类型时,它会动态获取它。

Not sure if there's a specific TypeScript way to deal with this, but you can use destructuring/spread for this:不确定是否有特定的 TypeScript 方法来处理这个问题,但您可以为此使用解构/传播:

const { type, ...validCredentials } = credentials;

This essentially picks type out of credentials, and validCredentials will contain the rest of the properties.这实质上是从凭证中挑选type ,而validCredentials将包含其余的属性。

The reason .filter didn't work is because filter is a prototype method on arrays, not objects. .filter的原因是因为 filter 是数组上的原型方法,而不是对象。

If you insist on using Array methods, .map() is probably most helpful:如果您坚持使用 Array 方法, .map()可能最有帮助:

 let obj2big = { a: 1, b: 2, c: 3 } // let's assume what you want from this is { a: 1, c: 3 } let arr = [obj2big] let result = arr.map(i=>({a:ia, c: ic})) console.log(result[0])

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM