回答:你不能这样做
从这里我了解到,要检查一个值是否为列表,你可以使用Array.isArray(),但我有一个奇怪的情况,我有一个查询函数
export async function query<T = unknown>(sql: string, options?: unknown): Promise<T> {
const pool = await initializePool()
const result = await pool.query(sql, options);
return /* T is of type list */ ? result : [result];
}
我无法在类型上使用Array.isArray(),我想知道是否有某种typeof函数可以用于T。
问题是,pool.query总是返回一个数组,如果可能的话,我想立即解构它。
const initializePool = async () => {
if (pool) {
return pool;
}
const CREDS = { connectionLimit: 500000, ...MYSQL_CREDS, multipleStatements: true }
pool = await mysql.createPool(CREDS)
return pool
}