[Typescript] Understanding TypeScript's Function Parameter Comparisons

Make those pass:

import { Equal, Expect } from "@total-typescript/helpers";

type Event = "click" | "hover" | "scroll";

type CallbackType = unknown;

const listenToEvent = (callback: CallbackType) => {};

listenToEvent(() => {});

listenToEvent((event) => {
  type tests = [Expect<Equal<typeof event, Event>>];
});

listenToEvent((event, x, y) => {
  type tests = [
    Expect<Equal<typeof event, Event>>,
    Expect<Equal<typeof x, number>>,
    Expect<Equal<typeof y, number>>,
  ];
});

listenToEvent((event, x, y, screenId) => {
  type tests = [
    Expect<Equal<typeof event, Event>>,
    Expect<Equal<typeof x, number>>,
    Expect<Equal<typeof y, number>>,
    Expect<Equal<typeof screenId, number>>,
  ];
});

 

You might be tempted to define the CallbackType as a function that either takes an Event by itself or along with x and y coordinates or screenId:

type CallbackType =
  | ((event: Event) => void)
  | ((event: Event, x: number, y: number) => void);
  | ((event: Event, x: number, y: number, screenId: number) => void);

However, with this change the first test call to listenToEvent with one parameter complains about an implicit any type:

listenToEvent((event) => { // red squiggly line under event
  type tests = [Expect<Equal<typeof event, Event>>]; // red squiggly line under Equal<>
})

// hovering over `event` shows:
Parameter 'event' implicitly has an 'any' type.

 

It turns out that because of the way TypeScript works, we can delete the first two members of the CallbackType union, keeping just the last one:

type CallbackType = (
  event: Event,
  x: number,
  y: number,
  screenId: number,
) => void;

With this change, all of the tests pass.

This might seem weird at first, but it makes sense when you consider how JavaScript works. Consider these function calls:

listenToEvent(() => {});

listenToEvent((event) => {
});

listenToEvent((event, x, y) => {
});

listenToEvent((event, x, y, screenId) => {
});

When implementing a function, it doesn't have to pay attention to everything that has been passed in. In each function call above, we pass the correct parameters, but the function does not necessarily use them. It can choose to ignore all parameters or pay attention to just the event, or the event and coordinates, or all four parameters.

This apply to callback function, not normal function

However, it cannot use a parameter that doesn't exist in its definition, as this would result in an error. This is why we needed to delete the first two members of the CallbackType union.

 

To further illustrate, let's look at another example of this concept.

Here we have an array of three numbers, and call map on it:

[1, 2, 3].map((num) => {
  console.log(num);
  return num;
});

The function passed to map only uses the num parameter, which represents the value. It ignores the index and entire array parameters that we could have passed in.

Just because a function can receive these parameters doesn't mean it has to handle them. This concept is crucial to understand when working with callbacks.

posted @   Zhentiw  阅读(6)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· AI技术革命,工作效率10个最佳AI工具
历史上的今天:
2020-08-06 [Machine Learning] Evaluation Metrics
2020-08-06 [CSS] Create Basic Effects with CSS box-shadow
2020-08-06 [Javascript] Natively Format JavaScript Dates and Times
2019-08-06 [Functional Programming] liftA2 and converge
2019-08-06 [Javascript] Convert a forEach method to generator
2019-08-06 [Flutter] Platform Checking
2019-08-06 [Flutter] Stateless widget and Stateful widget
点击右上角即可分享
微信分享提示