英文:
How to get the value type from a record in Typescript
问题
我有一个函数,它返回一个记录类型:ReturnType: Record<string, {...<SOME_BIG_TYPE>...}>,还有另一个函数,我想让它接受{...<SOME_BIG_TYPE>...}作为参数。我应该如何从记录类型中提取那个类型?
我想要的是类似下面这样的东西,其中ExtractedValueOf获取了我之前提到的那个值:
const myFunction = ({ bigObject }: { bigObject: ExtractedValueOf<ReturnType> }) => null;
我曾考虑过类似ReturnType<keyof ReturnType>的方法,但这并不起作用。
编辑:添加了一个基本示例来说明我的问题。
我这里有一个返回Record<string, SomeType>的函数,用来调用另一个函数,它接受SomeType作为参数。这都是类型安全的,符合我的预期:
type SomeType = {
field: string;
another: string;
};
function sample(): Record<string, SomeType> {
return {
object: {
field: "Hello",
another: "World",
},
};
}
function myFunction() {
return myOtherFunction(sample().object);
}
function myOtherFunction(sampleObject: SomeType) {
// 这里有一些操作
return sampleObject;
}
问题是,在我定义myOtherFunction的地方,我无法直接访问SomeType。我可以访问sample的返回类型,但我无法弄清楚如何从Record<string, SomeType>中获取SomeType。
英文:
I have a function which returns a record: ReturnType: Record<string, {...<SOME_BIG_TYPE>...}>, and another function that I would like to accept {...<SOME_BIG_TYPE>...} as an argument. How can I grab that type from the record?
I would like something like the following where ExtractedValueOf grabs that value I mentioned earlier.
const function = ({ bigObject }: { bigObject: ExtractedValueOf<ReturnType> }) => null;
I was thinking something like ReturnType<keyof ReturnType> but this does not work.
Edit: Added a basic example that illustrates my issue.
I have here a function that returns Record<string, SomeType>, which is used to call my other function, which takes an argument of SomeType. This is all typesafe and how I would expect it to work:
type SomeType = {
field: string;
another: string;
};
function sample(): Record<string, SomeType> {
return {
object: {
field: "Hello",
another: "World",
},
};
}
function myFunction() {
return myOtherFunction(sample().object);
}
function myOtherFunction(sampleObject: SomeType) {
// something in here
return sampleObject;
}
The problem is, in the place I have defined myOtherFunction, I don't have access to SomeType directly. I have access to the return type from sample, but I can't figure out how to get SomeType out of Record<string, SomeType>
答案1
得分: 0
你不需要这样做。
TypeScript 是 鸭子类型 的。只需将 myOtherFunction 定义为:
function myOtherFunction<T>(sampleObject: T) {...}
然后让编译器完成其工作。如果 myOtherFunction 需要参数的 another 属性为字符串,那么传递的任何参数都必须扩展 {another: NonNullable<string>}。
但如果你想要紧密耦合代码,以便 SomeType 的定义的任何更改都会独立影响这两个位置,那么你的问题就变成了:“在类型定义在编译时不可用时,如何进行静态代码评估?”
或者我是否漏掉了什么?
英文:
You don't need to.
Typescript is duck-typed. Just define myOtherFunction as
function myOtherFunction<T>(sampleObject: T) {...}
and let the tranpiler do its thing. If myOtherFunction requires the argument's another property to be a string then any argument being passed must extend {another: NonNullable<string>}.
But if you want to tightly couple the code so that any change in SomeType's definition affects both places independently then your question resolves to: "How can I do static code evaluation when the type definitions aren't available at compile time?"
Or am I missing something?
答案2
得分: 0
感谢h-sifat。我最初尝试过ReturnType[string],但结果评估为never。原来是因为在我的情况下,ReturnType要么是记录要么是未定义。修复方法是NonNullable<ReturnType>[string]。
英文:
Thanks very much to h-sifat. I did initially try ReturnType[string] before posting, but that evaluated to never. Turns out it was because ReturnType in my case was either the record or undefined. The fix was NonNullable<ReturnType>[string].
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。


评论