简体   繁体   English

通过在 Typescript 接口中的属性上声明的 key 获取泛型值的类型,并将其用作函数参数的类型

[英]Get type of generic's value by key declared on a property in Typescript interface and use it as the type of a function's parameter

I want to add a function signature on a typescript interface which one of its parameters will have a type based on the declared key's value.我想在 typescript 接口上添加 function 签名,其中一个参数将具有基于声明的密钥值的类型。

I've tried the approach bellow but it doesn't work, as the value 's type is every possible type from T .我尝试了下面的方法,但它不起作用,因为value的类型是T中所有可能的类型。 Instead, I want the value of the declared key相反,我想要声明键的值

so if key "num" is a number, I want the "value" parameter in function to be a number所以如果键“num”是一个数字,我希望 function 中的“value”参数是一个数字


type RowType = Record<string, any>

interface Row<T = RowType, K extends keyof T = keyof T> {
    key: K
    render: (value: T[K], row: T) => any
}

interface DumpRecord = {
    arr: string[], 
    num: number, 
    str: string
}

let row: Row<DumpRecord> = {
 key: 'arr',
 render: (value, row) => ... // value should be array, row should be DumpRecord
}

// or

let row: Row<DumpRecord> = {
 key: 'num',
 render: (value, row) => ... // value should be number
}


You need to use discriminated unions here:您需要在这里使用有区别的联合

type RowType = Record<string, any>

interface Row<T = RowType, K extends keyof T = keyof T> {
    key: K
    render: (value: T[K], row: T) => any
}

interface DumpRecord {
    arr: string[],
    num: number,
    str: string
}
type Values<T> = T[keyof T]

type Union = Values<{
    [Prop in keyof DumpRecord]: Row<DumpRecord, Prop>
}>

const one: Union = {
    key: 'arr',
    render: (value, row) => {
        value // string[]

    }
}

const two: Union = {
    key: 'num',
    render: (value, row) => {
        value // number
    }
}

Playground操场

You can find more examples in this question and my article您可以在这个问题和我的文章中找到更多示例

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 为接口的函数属性实现泛型类型 - Implement a generic type for an interface's function property 在 TypeScript 中获取接口属性的类型 - Get type of an interface's property in TypeScript TypeScript:通过读取泛型类型的值来键入泛型接口的方法参数 - TypeScript: Typing generic interface's method parameter by reading a value from the generic type Typescript 获取泛型函数的返回类型 - Typescript get generic function's return type Typescript 使用 generics 使用类型键参数从接口获取类型值? - Typescript use generics to get type-value from interface using type-key parameter? 参数类型作为通用接口中键的值 - Parameter type as value of key in generic interface Typescript 默认 function 参数基于泛型的类型 - Typescript default function parameter based on generic's type 如何在导入的接口内向已声明函数的参数类型添加属性? TS2322 - How can i add property to already declared function's parameter type inside imported interface? TS2322 如何让 TypeScript 通用 function 类型参数是 object 的键值的返回类型 - How to have TypeScript generic function with type argument being keyof an object have return type of that object's key's value 基于泛型的类型(函数参数的类型) - Type based on generic (function parameter's type)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM