admin管理员组文章数量:1391943
I'm trying to create a strongly typed function in TypeScript that infers the type of params.srcData based on the src function inside the same object.
Here’s what I have so far:
import { Observable } from 'rxjs';
type GetSrc<T> = T extends { src: () => Observable<infer TSrc> } ? TSrc : never;
function myFunction4<
TData,
T extends {
[key in keyof T]: {
src: () => Observable<unknown>;
} extends any
? {
src: () => Observable<GetSrc<T[key]>>;
api: (params: { srcData: GetSrc<T[key]> }) => Observable<TData>;
}
: never;
}
>(obj: T) {
return obj;
}
const result4 = myFunction4({
test1: {
src: () => new Observable<number>(),
api: (params) => {
// I would like params.srcData to be inferred as "number"
const srcData = params.srcData;
return new Observable<number>();
},
},
test2: {
src: () => new Observable<string>(),
api: (params) => {
// I would like params.srcData to be inferred as "string"
const srcData = params.srcData;
return new Observable<number>();
},
},
} as const);
Expected Behavior:
params.srcData in test1.api should be inferred as number params.srcData in test2.api should be inferred as string
Actual Behavior:
params.srcData is inferred as never
Question:
How can I correctly infer the type of params.srcData from src in my function definition? What adjustments are needed in my TypeScript generic types?
Here a link where I reproduce the problem: ;file=index.ts
I'm trying to create a strongly typed function in TypeScript that infers the type of params.srcData based on the src function inside the same object.
Here’s what I have so far:
import { Observable } from 'rxjs';
type GetSrc<T> = T extends { src: () => Observable<infer TSrc> } ? TSrc : never;
function myFunction4<
TData,
T extends {
[key in keyof T]: {
src: () => Observable<unknown>;
} extends any
? {
src: () => Observable<GetSrc<T[key]>>;
api: (params: { srcData: GetSrc<T[key]> }) => Observable<TData>;
}
: never;
}
>(obj: T) {
return obj;
}
const result4 = myFunction4({
test1: {
src: () => new Observable<number>(),
api: (params) => {
// I would like params.srcData to be inferred as "number"
const srcData = params.srcData;
return new Observable<number>();
},
},
test2: {
src: () => new Observable<string>(),
api: (params) => {
// I would like params.srcData to be inferred as "string"
const srcData = params.srcData;
return new Observable<number>();
},
},
} as const);
Expected Behavior:
params.srcData in test1.api should be inferred as number params.srcData in test2.api should be inferred as string
Actual Behavior:
params.srcData is inferred as never
Question:
How can I correctly infer the type of params.srcData from src in my function definition? What adjustments are needed in my TypeScript generic types?
Here a link where I reproduce the problem: https://stackblitz/edit/bskqm7z9?devtoolsheight=50&file=index.ts
Share Improve this question asked Mar 14 at 7:45 RomainGRomainG 5975 silver badges13 bronze badges 1- If you need more engagement here, consider editing your minimal reproducible example to be pure TS without a dependency on rxjs or any external/third-party frameworks. – jcalz Commented Mar 14 at 22:10
1 Answer
Reset to default 1Given high order functions, input and output infers and an argument object with different value types it's hard to provide solution with exact call signature you are using. But there's an alternative to use separate method calls to fill the object with a probably simpler approach of using a generic type for an api pair (you could give the class a better name):
Playground
import { Observable } from 'rxjs';
type ApiPair<T, R> = {
src: () => Observable<T>;
api: (params: { srcData: T }) => Observable<R>;
};
class Api{
add<T, K extends string, R>(key: K, pair: ApiPair<T,R>): asserts this is this & {[k in K]: ApiPair<T, R>}{
(this as any)[key] = pair;
}
}
const api: Api = new Api;
api.add('test1', {
src: () => new Observable<number>(),
api: (params) => {
// I would like params.srcData to be inferred as "number"
const srcData = params.srcData;
return new Observable<number>();
},
});
api.add('test2',{
src: () => new Observable<string>(),
api: (params) => {
// I would like params.srcData to be inferred as "string"
const srcData = params.srcData;
return new Observable<number>();
},
});
api.test2 // (property) test2: ApiPair<string, number>
本文标签:
版权声明:本文标题:typescript - How to infer a nested property type and use it in another property within the same object? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744668884a2618702.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论