Sto usando il mssql libreria che ha questo interface
:
export interface IRecordSet<T> extends Array<T> {
columns: IColumnMetadata;
toTable(name?: string): Table;
}
Ho una funzione che riceve i dati da un database e restituisce un array di IRecordSet<T>
una matrice di matrici che contengono il tipo generico <T>
. Questo appare come:
[[{}, {}, ...], [{}, {}, ...], ...]
import { IRecordSet } from 'mssql'
type Data<T> = Array<IRecordSet<T>>
async function getData (sql: string): Promise<Data<any>> {
// connect to db, run sql
return []
}
Ora ho bisogno di una funzione che chiama getData()
e mi piacerebbe tipo effettiva dati restituiti da fornire il tipo generico in IRecordSet<T>
.
So che questo non funziona, ma questo è quello che ho adesso:
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]
// ...
}
Dattiloscritto dice:
Type '[BookData, CarData]' is not assignable to type 'BookData'.
Type 'IRecordSet<[BookData, CarData]>' is not assignable to type 'CarData[]'.
Capisco questi errori, ma non so come tipo il myData
, firstBook
& cars
variabili utilizzando le interfacce definite (BookData
& CarData
).
Che cosa dovrebbe type BooksAndCars = Data<[BookData, CarData]>
essere..?