@goodrequest/joi-type-extract

中文文档
0.0.5发布于 2025/10/06MITTypeScript近 30 天下载 3353

Solution that extractes types from joi schemas.

原始使用文档

joi-type-extract

Clone of https://github.com/TCMiranda/joi-extract-type

Setup

  1. Install package
    npm install @goodrequest/joi-type-extract
    or
    yarn add @goodrequest/joi-type-extract
    
  2. Create ./src/types/joi/index.d.ts and import package in it, which will extend Joi types
    import '@goodrequest/joi-type-extract'
    
  3. Add this compiler option and include created file in tsconfig.json in typeRoots
    {
        "compilerOptions": {
            "strictNullChecks": true,
            "typeRoots": [
                "./src/types"
            ]
        }
    }
    

Usage example

import Joi from 'joi'

export const requestSchema = () =>
    Joi.object({
        params: Joi.object({
            userID: Joi.number().required()
        }),
        query: Joi.object({
            search: Joi.string().required()
        }),
        body: Joi.object({
            name: Joi.string().required()
        })
    })

export const responseSchema = Joi.object({
   messages: Joi.array().items({
       message: Joi.string().required(),
       type: Joi.string().valid(MESSAGE_TYPE.SUCCESS).required()
   }).required()
})

type RequestType = Joi.extractType<ReturnType<typeof requestSchema>>
type ResponseType = Joi.extractType<typeof fullMessagesResponseSchema>

Extracted EequestType and ResponseType can be then used in express.Request and express.Response types

import { Request, Response, NextFunction } from 'express'

const businessLogic = async (
    req: Request<RequestType['params'], ResponseType, RequestType['body'], RequestType['query']>,
    res: Response<ResponseType>,
    next: NextFunction
) => {
    const { params, query, body } = req

    // params.userID will have number type
    // query.serch will have string type
    // body.name will have string type

    // response will be enforced to be same as ResponseType
    return res.json({
        messages: [
            {
                message: 'Response message',
                type: MESSAGE_TYPE.SUCCESS
            }
        ]
    })
}

版本与发布记录

@goodrequest/joi-type-extract 共发布 4 个版本,首次发布于 2022/06/10;过去 12 个月发布了 0 个版本。

版本发布日期距今
0.0.5latest2025/10/061.1 年前
0.0.42023/01/203 年前
0.0.32022/08/304 年前
0.0.22022/06/104 年前

关于 @goodrequest/joi-type-extract 的常见问题

@goodrequest/joi-type-extract 是什么?

Solution that extractes types from joi schemas.

如何安装 @goodrequest/joi-type-extract?

在项目目录运行 npm install @goodrequest/joi-type-extract,或使用 pnpm add / yarn add / bun add。

@goodrequest/joi-type-extract 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@goodrequest/joi-type-extract 可以商用吗?

@goodrequest/joi-type-extract 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@goodrequest/joi-type-extract 的下载量有多少?

根据 npm 官方下载统计,@goodrequest/joi-type-extract 在 2026/10/17 – 2026/11/15 这 30 天内被下载 3,353 次,日均约 112 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@goodrequest/joi-type-extract 还在维护吗?

最新版本 0.0.5 发布于 2025/10/06(1.1 年前);至今共发布 4 个版本,其中过去 12 个月发布了 0 个;首次发布于 2022/06/10。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。