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

typescript - How to infer a nested property type and use it in another property within the same object? - Stack Overflow

programmeradmin2浏览0评论

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
Add a comment  | 

1 Answer 1

Reset to default 1

Given 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>

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论