typescript - 使用TypeScript,如何强烈键入mysql查询结果

标签 typescript

我是Typescript的新手,无法确定我是否在正确输入查询结果。这是我的代码的本质...

import mysql2, {Pool} from "mysql2";
const pool: Pool = mysql2.createPool({...}).promise();

interface IUser {
    uid   : number;
    uname : string;
}

class UserController {

    public async getUser(id: number): Promise<IUser> {
        const [rows]: Array<{rows: IUser}> = await pool.query(
            "SELECT * FROM `users` WHERE `email` = ?",["me@me.org"]);        
        return rows;
    }
}

TypeScript编译器(3.3.1)提示我的return rows语句。

TS2740: Type '{rows: IUser;}' is missing the following properties from type 'IUser': uid and uname.



如果我忽略// @ts-ignore的返回,那么一切都很好。我把我的物体放回原处,没有任何错误。

难道我做错了什么?

我做了一些更改,但是对于TypeScript为什么不提示,我实在感到困惑。似乎根本不对。
    class UserController {

        public async getUser(id: number): Promise<{rows: IUser}> {
            const [rows]: Array<{rows: IUser}> = await pool.query(
                "SELECT * FROM `users` LIMIT 3");        
            return rows;
        }
    }

这是正确的吗???

所以,没有,这全都错了。

当我想到query返回[rows, fields]时,它也开始变得更有意义了。我认为@ joesph-climber经过一些调整后的语法是正确的。

这对我来说很有用...
    class UserController {

        public async getUser(id: number): Promise<Array<{rows: IUser}>> {
            const [rows]: [Array<{rows: IUser}>] = await pool.query(
                "SELECT * FROM `users` LIMIT 3");        
            return rows;
        }
    }

这也可行,并且可能更容易理解。
    class UserController {

        public async getUser(id: number): Promise<IUser[]> {
            const [rows]: [IUser[]] = await pool.query(
                "SELECT * FROM `users` LIMIT 3");        
            return rows;
        }
    }

最佳答案

// the function should return a Promise that resolves to 
{ uid: number, uname: string }

// but typeof rows is 
{ rows: { uid: number, uname: string } }

执行该示例,我得到的结果是这样的:
[ TextRow { uid: 1, uname: 'foo', email: 'foo@mail.com' } ]

因此,pool.query返回一个以IUser数组作为第一个元素的数组。

Returning multiple users:


class UserController {
    // returns all users
    public async getUsers(): Promise<Array<IUser>> {
        const [rows]: [Array<IUser>] = await pool.query(
                "SELECT * FROM `user`", []);
        return rows; // rows is Array<IUser> so it matches the promise type
    }
}

Returning a specific user:


class UserController {
    public async getUser(id: number): Promise<IUser> { // Note the 
        const [rows]: [Array<IUser>] = 
            await pool.query("SELECT * FROM `user` WHERE `email` = ?",
                            ["foo@mail.com"]);
        // If email is unique as it would be expected it will 
        // return a single value inside an array
        // rows is still Array<IUser>
        return rows[0]; // rows[0] is an IUser 
    }
}

关于typescript - 使用TypeScript,如何强烈键入mysql查询结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54583950/

相关文章:

class - 为什么我不能 "implements"TypeScript 2.4+ 中的全可选接口(interface)?

angular - 元素访问表达式应该有一个参数

c# - 检查 List<T> 的项目是否有效

javascript - Angular 2 与 TypeScript : View not updating after setting variable in response handler

javascript - React.js import 语句找不到模块

typescript - 如何配置 VS Code 自动导入?

javascript - 对对象键/值使用多个映射类型。只需要联合的一个值?

typescript - 使用类表达式时是否支持循环引用?

javascript - 使用 React useContext 和 useReducer 时出现 Typescript 错误

typescript - 代码重复检测是否适用于 SonarQube 中的 TypeScript 语言?