【问题标题】:Typescript Types - question about composite types using inferTypescript Types - 关于使用推断的复合类型的问题
【发布时间】:2019-09-10 22:17:52
【问题描述】:

使用redux,我最终在我的商店代码中编写了以下sn-p:

type CombinedParamTypes<T extends {
    [key: string]: (state: any, action: any) => any;
}> = T extends {
    [key: string]: (state: infer R, action: any) => any;
} ? R : never;

type CombinedReturnTypes<T extends {
    [key: string]: (...args: any) => any;
}> = T extends {
    [key: string]: (...args) => infer R;
} ? R : never;

例子:

import camera from "./camera/reducer";
import settings from "./settings/reducer";

export const ALL_REDUCERS = {
    camera,
    settings,
};

const COMBINED_REDUCERS = combineReducers(ALL_REDUCERS);

export type FlatReduxState = CombinedParamTypes<typeof ALL_REDUCERS>;
// returns intersection type: ICameraState & ISettingsState

export type WhyDifferent = CombinedReturnTypes<typeof ALL_REDUCERS>;
// returns union type: ICameraState | ISettingsState

谁能解释为什么他们返回的方式不同?我意识到一个正在查看参数,另一个正在查看返回类型,但这如何转化为决定交集与联合?

【问题讨论】:

  • 询问需要使用什么参数来调用函数联合将始终是参数类型的交集。 IE如果我问打字稿我需要什么参数来调用函数F1和F2的联合,它将是F1和F2的参数如果我问打字稿,ReturnType是不一样的F1的ReturnType是什么| F2 它将是返回类型的联合,因为它要么不是两者。

标签: reactjs typescript redux react-redux


【解决方案1】:

函数类型为covariant in their return type and contravariant in the argument types。关于type inference in conditional types,文档中也有说明:

[...] 同一类型变量在协变位置的多个候选会导致联合类型被推断 [...] 同样,同一类型变量在反变位置的多个候选会导致交集类型待推断[.]

对于你的 reducer 返回类型,TypeScript 推断出R 的所有实例化的公共超类型,这导致返回类型的联合类型 - ICameraState | ISettingsState。反过来,CombinedParamTypes 的函数参数中 R 的所有实例都被组合为交集类型,以获得R - ICameraState &amp; ISettingsState 的公共 子类型

当您第一次(和第二次 - 对我说......)第一次听到协变和逆变的基本概念时,有时会有点难以理解。 sens 中的协变意味着,当基本组件类型中的每一个都打包成更复杂的类型(高阶类型/HOT)时,如函数、列表或其他类型,它们的子类型关系被保留。逆变是相反的 - 子类型成为 HOT 中的超类型,因为函数的参数类型是逆变的。

遵循this recommended article的狗和动物类比的简单示例:

type Animal = {
  sex: "m" | "w";
};

type Dog = {
  bark(): void;
};

declare const animals: {
  aDog: (d: Dog) => string;
  anAnimal: (a: Animal) => number;
};

// string | number is the supertype of all given return types (covariance)
type ReturnTypes = CombinedReturnTypes<typeof animals>; // string | number

// Dog & Animal is the subtype of all given function parameter types (contravariance)
type Params = CombinedParamTypes<typeof animals>; // Dog & Animal

function testCombinedParamTypes(arg: (p: Params) => void) {
  // assumes that the argument of callback is Dog AND Animal...
  arg({ sex: "m", bark: () => "wuff" });
}

function client() {
  // ... so it is safe for a client to pass in a callback
  // that deals only with a Dog XOR Animal (see the original typeof animals   
  // type from which Params is derived). E.g. Dog being a super type of Dog & Animal 
  // can be safely passed as argument in a contravariant position 
  testCombinedParamTypes((dog: Dog) => {});
}

Playground

希望,我能把事情弄清楚一点。

【讨论】:

  • 很好的解释,谢谢!但是,您的“条件类型中的类型推断”URL 不起作用。
  • 谢谢!更新了答案中的链接。
猜你喜欢
  • 2016-04-04
  • 1970-01-01
  • 2020-02-16
  • 2018-07-26
  • 2017-08-24
  • 2021-03-01
  • 2020-10-14
  • 2018-07-03
  • 2016-03-13
相关资源
最近更新 更多