简体   繁体   English

在打字稿泛型类型声明中遇到问题

[英]Having trouble in typescript generic type declaration

I'm creating a hook called useUpdate , having trouble declaring the type, the code shows below我正在创建一个名为useUpdate的钩子,在声明类型时遇到问题,代码如下所示

type CallBack<T extends readonly any[]> = (
  ...args: T
) => void | (() => void | undefined);

function useUpdate<T extends readonly any[]>(
  callback: CallBack<T>,
  deps: T
) {
  const didMount = useRef(false);
  const prevDeps = useRef(deps);

  useEffect(() => {
    if (!didMount.current) {
      didMount.current = true;

      return;
    }

    const clean = callback(...prevDeps.current);

    prevDeps.current = deps;

    return () => {
      if (clean) {
        clean();
      }
    };
  }, deps);
}

when using it there's no problem if the callback has args, but if no args is provided for the callback, the type of deps will be incorrect使用时如果回调有args没有问题,但是如果回调没有提供args,则deps的类型会不正确

const state: boolean = useSomethingElse(); // get changable data from anywere else

// OK, no type error
useUpdate((prevState) => {
 console.log('I want to compare the current state and prevState', prevState, state);
},[state]);


useUpdate(() => {
 console.log('I only care about the current state', state);
},[state]); // Type error occurs on this line, the message is:
//  Argument of type '[boolean]' is not assignable to parameter of type '[]'.
//  Source has 1 element(s) but target allows only 0.ts(2345)

I know it's because TypeScript sets the T as callback args which is [] if the args are not provided, but is there any way to let TypeScript uses the type of deps and skips the type of callback args while setting the T?我知道这是因为 TypeScript 将 T 设置为回调参数,如果未提供参数,则为 [],但是有没有办法让 TypeScript 在设置 T 时使用 deps 的类型并跳过回调参数的类型?

Two ways you can fix this:有两种方法可以解决此问题:

(1) Make deps the first parameter so it starts by inferring T from deps (1) 使deps成为第一个参数,因此它首先从deps推断T

(2) Use a second generic for callback : (2) 对callback使用第二个泛型:

function useUpdate<T extends readonly any[], Cb extends CallBack<T>>(
  callback: Cb,
  deps: T
) {

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM