最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

typescript - Is there any way to infer a type from a Typed Map[]? - Stack Overflow

programmeradmin1浏览0评论

I'm using TypedMap for creating a QueryBuilder, so i need to make a { value, field, method } for conditions, and retrieve the correct value relative to method. i'm using a Array MapType

export type WhereConditions<
    TablesMap extends { [k: string]: { fields: TablesMap[typeof k] } },
    TableName extends keyof TablesMap,
    TableFields extends keyof TablesMap[TableName]["fields"]
> = {
    [K in TableFields]: {
        field: K,
        method: TablesMap[TableName]["fields"][K] extends string
        ? keyof typeof WhereMethods.string
        : TablesMap[TableName]["fields"][K] extends Date
        ? keyof typeof WhereMethods.date
        : keyof typeof WhereMethods.number;
        value: /* Infer value from method */
    }
}[TableFields][]

So, once the QueryBuilder is Called, first is setted the TableName, with a Map we retrieve the fields for select, and optionally, we can make conditions on this call

.from("TECVERDE.EST_PRODUTOS@Tecverde")
                .select("nome")
                .where(
                    [
                        {
                            field: "nome",
                            method: "in",
                            value: /** Expected string[] */
                        },
                        {
                            field: "nome",
                            method: "equal",
                            value: /** Expected string */
                        }

So, if method is in, value can only receive string[], if equal, can only receive string. The WhereReferenceMap is

string:{
        equal: (value: string) => `= '${value}'`,
        in: (value: string[]) => `IN (${value.map(v => `'${v}'`).join(", ")})`,
} 

发布评论

评论列表(0)

  1. 暂无评论