我是typescript的新手,了解它的基础知识。但是当我在项目中使用包时,我对节点包、注解和描述这些类型的最佳实践有点困惑。
我的意思是,真的需要描述一个包的返回注解,或者在包使用的参数中指定数据类型吗?
让我们以mysql 2包为例。
`
const poolFunction = async (SQL: string): Promise < [RowDataPacket[] | RowDataPacket[][] | OkPacket | OkPacket[] | ResultSetHeader, FieldPacket[]] > => {
return await pool.query(SQL);
}
或表达...
const routeFunction = (req: Request, res: Response): void => {
}
`
当然,它是非常描述性的,但它是必要的吗?
只是试着理解,并希望在未来的打字稿!
1条答案
按热度按时间e4eetjau1#
是否真的需要描述一个包的返回注解,或者在它们的参数中指定数据类型,当它们已经用下载的@类型定义时,这些参数被包使用?
在你的例子中,是的,因为TypeScript不能知道
routeFunction
是否要和某个Express函数一起使用。通过隔离它,你必须键入参数,尽管返回类型可以推断出来。如果您传送 expected function,TypeScript将会推断参数的型别,例如: