简体   繁体   English

TypeScript:对于这个 function,如何将类型显式传递给 generics

[英]TypeScript: How can I explicitly pass in the type to the generics for this function

I am learning TypeScript and I was trying to use mapped types to write a function that only allows users to extract values off of the object when the keys indeed exist on the object. I am learning TypeScript and I was trying to use mapped types to write a function that only allows users to extract values off of the object when the keys indeed exist on the object. Here is the code这是代码

const obj = {
    a: 1,
    b: 2,
    c: 3
}

function getValues<T, K extends keyof T>(obj: T, keys: K[]) {
    return keys.map(key => obj[key])
}

getValues(obj, ['a', 'b'])

So here I defined two type parameters T and K and they are inferred by the TS compiler because when I called the function I didn't explicitly pass in the types.所以在这里我定义了两个类型参数TK ,它们是由 TS 编译器推断出来的,因为当我调用 function 时,我没有显式地传入类型。

Now my question is, what if I want to explictly pass the types into the function, how should I rewrite the function?现在我的问题是,如果我想明确地将类型传递给 function,我应该如何重写 function? I want to do this because I am curious about how the type inference works here我想这样做是因为我很好奇类型推断在这里是如何工作的

so this is my attempt所以这是我的尝试

getValues<typeof obj, string[] extends keyof typeof obj>(obj, ['a'])

However the compiler is not happy.但是编译器并不高兴。 There is an parsing error Parsing error: '?' expected存在解析错误Parsing error: '?' expected Parsing error: '?' expected

Assuming you really want to pass a type in explicitly, I don't think using extends is allowed in the invocation of the function.假设您真的想显式传递一个类型,我认为在 function 的调用中不允许使用extends

getValues<typeof obj, keyof typeof obj>(obj, ['a'])

暂无
暂无

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

相关问题 Typescript:`let result:{[key:T [K]]:T} = {};`不起作用,如何基于泛型键入对象? - Typescript: `let result: { [key: T[K]]: T } = {};` is not working, how can I type my object based on generics? 如何使用Typescript将类变量传递到函数内部的函数中? - How can I pass a class variable into a function inside a function with typescript? Typescript:如何正确使用 Generics 来正确推断 Function 的返回类型? - Typescript: How To Use Generics Properly To Infer Return Type of Function Correctly? 将 object 传递给 function 时,如何指定 TypeScript 类型? - How can I specify a TypeScript type when passing an object into a function? Typescript:如何在不重新定义类型的情况下将复杂类型的变量传递给 function - Typescript: how to pass variable with complex type to a function without redefining type 我该如何重写此功能,而不必显式键入范围内的每个IP地址? - How can I rewrite this function so I don't have to explicitly type out every IP address within a range? 如何在 Typescript 中传递道具? - How can I pass props in Typescript? 如何将 generics 类型传递给 React Hooks useReducer - How to pass generics type to React Hooks useReducer 在打字稿中,我将如何输入这个“撰写”功能 - In Typescript how would I type this 'compose' function 如何将接口/类型动态传递给打字稿中的常见反应函数 - How to pass interface/type dynamically to a common react function in typescript
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM