TS中难以理解的诡异行为,下面的四种写法为什么会结果一样呢?

type GetReturnType1<Type> = Type extends (...args: never) => infer ABC
  ? ABC
  : never;

type GetReturnType2<Type> = Type extends (...args: never[]) => infer ABC
  ? ABC
  : never;
type GetReturnType3<Type> = Type extends (...args: any[]) => infer ABC
  ? ABC
  : never;


 type GetReturnType4<Type> = Type extends (...args: any) => infer ABC
  ? ABC
  : never; 

还有下面的一个代码,本来想着T 不是Major 就是 ResCategoryLabel。但是setSelect地方会报错,条件类型与联合类型不匹配。怎么想也觉得没有问题,不得不加ts-ignore忽略,很不理解。哪位大佬来给讲讲,学习一下。


type Props<T extends Major | ResCategoryLabel> = {
  labels: T[]; 
  setSelect: (
    index: number,
    label: T extends Major ? Major : ResCategoryLabel
  ) => void; 
  xxx
};
  const changeSelect = (
    index: number,
    label: Major | ResCategoryLabel,
    e: React.MouseEvent<HTMLAnchorElement> | React.TouchEvent<HTMLAnchorElement>
  ) => {
    setSelect(index, label); 
    activeTabToCenter(e.currentTarget as HTMLElement);
  };

阅读 1k
推荐问题
logo
Microsoft
子站问答
访问
宣传栏