【问题标题】:Typescript Definitions - nested functions打字稿定义 - 嵌套函数
【发布时间】:2019-08-03 05:09:31
【问题描述】:

如下所列,假设我们有:

  1. 一个函数r将多个任务组合在一起
  2. 和一个函数o,它返回一个类似when(cb).map(cb)的形状

传递给when 或map 的每个回调都应始终采用这些3 参数:S, A, C 其中S 和C 在r 中定义,A 在o 中定义.

这里是typescript playground 的链接,它也显示了我遇到的错误。

我的问题是:如何获得类型安全声明?

type Task<S, A, C> = <AA extends A>(s: S, a: AA, c: C) => any;
type Fn<S, A, C> = (s: S, a: A, c: C) => any;

const r = <S, C>() => ({
  tasks: (...tasks: Task<S, any, C>[]) => null,
});

const o = <T, A = { prop: T }>(type: T) => ({
  when: <S, C>(fp: Fn<S, A, C>) => ({
    map: <SS extends S, CC extends C>(fn: Fn<SS, A, CC>): Task<SS, A, CC> => (
      (s: SS, a: A, c: CC): any => (
        fp(s, a, c) ? fn(s, a, c) : s
      )
    ),
  }),
});

const result = r<2, 7>().tasks(
  o(44) // expect: cb(2, 44, 7)
    .when((s, a, c) => s + a.prop + c)
    .map((s, a, c) => s + a.prop + c),

  o(78) // expect: cb(2, 78, 7)
    .when((s, a, c) => s + a.prop + c)
    .map((s, a, c) => s + a.prop + c),

  // etc...
  // callback provided to `.map` is typesafe,
  // callback provided to `.when` is not,
);

如您所见,提供给when 的回调不是类型安全的:参数S 和C 丢失。

【问题讨论】:

    标签: javascript typescript typescript-typings typescript-types typescript-definitions


    【解决方案1】:

    嗯,除其他问题外,您似乎想要一些语言不提供的contextual type inference。以下是我推荐的输入方式:

    type Fn<S, A, C> = (s: S, a: A, c: C) => any;
    
    // allow tasks to be an array of Fn<S, any, C> 
    const r = <S, C>() => ({
      tasks: <FF extends Fn<S, any, C>[]>(...tasks: FF) => null,
    });
    // it is concerning that S and C must be explicitly specified
    // when calling r(); not sure how you will hook that up to runtime
    
    // only make types generic if they need to be, 
    // and declare close to their uses if you want them to be inferred
    const o = <T>(type: T) => ({
      when: <S, C>(fp: Fn<S, { prop: T }, C>) => ({
        map: (fn: Fn<S, { prop: T }, C>) => (s: S, a: { prop: T }, c: C): any =>
          fp(s, a, c) ? fn(s, a, c) : s,
      }),
    });
    
    const result = r<2, 7>().tasks(
      o(44) // expect: db(2, 44, 7)
        // you need to annotate the s and c parameters in when().
        // The compiler does not try to infer S and C in o(44).when() contextually 
        // from the contextual parameter type of r<2.7>().tasks().
        .when((s: 2, a, c: 7) => s + a.prop + c)
        .map((s, a, c) => s + a.prop + c),
    
      o(78) // expect: db(2, 78, 7)        
        // alternatively you can specify S and C manually:
        .when<2, 7>((s, a, c) => s + a.prop + c)
        .map((s, a, c) => s + a.prop + c),
      // etc...
    );
    

    自从我处理此问题以来,您已经更改了定义,因此以下内容可能与您发布的内容不完全匹配。简而言之:

    • 使r().tasks() 采用Fn&lt;S, any, C&gt; 值的数组(可能是tuple),因此第二个任务的A 与第一个任务不同这一事实不会导致错误。

    • 没有通用的T 和通用的A = {prop: T}。我猜A 并不意味着独立于T 并且您正在尝试使用default type parameter 来表示某种分配,但它并没有真正这样工作。相反,只需使用T,然后将A 的所有实例替换为{prop: T}。

    • 只拥有尽可能多的泛型类型,并且尽可能靠近所需的推理位置。我已将S 和C 移至o().when。

    • 最后,从r&lt;2.7&gt;().tasks() 的参数到o().when() 中S 和C 的值的上下文类型不会发生。编译器甚至可能不会尝试这样做,因为推理必须跨越多个级别的函数调用。处理这个问题的唯一方法似乎是重新指定S 和C,或者通过注释传递给o().when() 的回调的s 和c 参数,或者通过调用o().when&lt;2,7&gt;()。

    希望这有助于为您指明正确的方向。祝你好运!

    【讨论】:

    • 嗨@jcalz,感谢您指出类型分配。之所以在r 中定义S 和C,是因为r 表示已经存在的东西,它总是会用相同的S 和S 和C 调用所有任务:任务无法控制它。我还没有真正理解为什么map 输入正确而when 不是...
    • 因为when() 是一个泛型函数(它具有S 和C 作为类型参数),它返回一个具有非泛型map() 函数的对象。当您调用o(44).when&lt;2,7&gt;() 时,返回的对象的map() 已经没有未指定的泛型类型参数。它的fn 参数已知是(s: 2, a: {prop: number}, c: 7) =&gt; any 类型。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-08-21
    • 2019-07-30
    • 2023-01-05
    • 2020-05-02
    • 2019-07-30
    • 2021-12-20
    • 1970-01-01
    相关资源
    最近更新 更多