我使用的 mssql 库,它具有这个 interface
:
export interface IRecordSet<T> extends Array<T> {
columns: IColumnMetadata;
toTable(name?: string): Table;
}
我有一个功能,获取数据库中的数据和返回的一个系列 IRecordSet<T>
所以一系列阵列含有一般类型 <T>
. 这看起来像:
[[{}, {}, ...], [{}, {}, ...], ...]
import { IRecordSet } from 'mssql'
type Data<T> = Array<IRecordSet<T>>
async function getData (sql: string): Promise<Data<any>> {
// connect to db, run sql
return []
}
现在我需要一个功能的电话 getData()
我喜欢的类型实际返回的数据,通过提供通用类型 IRecordSet<T>
.
我知道这不起作用,但是这是什么我现在:
interface BookData {
name: string
author: string
}
interface CarData {
make: string
model: string
}
type BooksAndCars = Data<[BookData, CarData]>
async function getBooksAndCars (): Promise<void> {
const myData: BooksAndCars = await getData(`
SELECT name, author FROM Books;
SELECT make, model FROM Cars;
`)
const firstBook: BookData = myData[0][0]
const cars: CarData[] = myData[1]
// ...
}
稿是说:
Type '[BookData, CarData]' is not assignable to type 'BookData'.
Type 'IRecordSet<[BookData, CarData]>' is not assignable to type 'CarData[]'.
我了解这些错误,但我不知道如何类型的 myData
, firstBook
& cars
变量使用的接口定义(BookData
& CarData
).
应该什么 type BooksAndCars = Data<[BookData, CarData]>
是..?