慎用!!!不要在Typescript中使用Function类型

开发 前端
事实上,我们已经舍弃了所有类型声明,但 video​仍旧被推断为 { name: string; views: number } ​。这是可能的,因为我们的函数定义的特殊性:(item: T) => number 。

原文链接:https://www.totaltypescript.com/dont-use-function-keyword-in-typescript

翻译:一川

在Typescript中不应该使用Function作为一个类型,因为它可以表示任何函数。通常,我们期望的是更具体的类型--例如指定参数的数量或函数返回的内容。如果确实要表示可以接受任意数量的参数并返回任何类型的函数,请使用 (...args: any[]) => any。

1、完整的解释

假设您正在创建一个汇总对象数组的函数。这是一个,取自Excalidraw代码库:

const sum = <T>(
  array: readonly T[],
  mapper: (item: T) => number
): number =>
  array.reduce(
    (acc, item) => acc + mapper(item),
    0
  );

让我们看一下类型定义。此函数包含:

  • 只读数组:readonly T[]
  • 映射器函数:(item: T) => number

并返回 number类型。

在主体中,它调用array.reduce(func, 0)。这意味着在acc

对于数组的每个成员,它通过将acc和mapper(item)进行相加,最终得到数组所有成员的总和。

2、什么可以用来作为函数声明?

mapper函数是关键。我们来剥离一下来看看:

type Mapper<T> = (item: T) => number;

让我们想象一个用例:

interface YouTubeVideo {
  name: string;
  views: number;
}
 
const youTubeVideos: YouTubeVideo[] = [
  {
    name: "My favorite cheese",
    views: 100,
  },
  {
    name: "My second favorite cheese (you won't believe it)",
    views: 67,
  },
];
 
const mapper: Mapper<YouTubeVideo> = (video) => {
  return video.views;
};
 
const result = sum(youTubeVideos, mapper); // 167

3、关于什么是函数?

我看到很多初学者开发人员犯的一个大错误,声明一个类似于 mapper和Function类型 的函数:

const sum = <T>(
  array: readonly T[],
  mapper: Function
): number =>
  array.reduce(
    (acc, item) => acc + mapper(item),
    0
  );

这个关键字基本上代表“任何函数”。这意味着从技术上讲可以 sum 接收任何函数。

当在 中使用 sum 时,我们失去了很多 (item: T) => number 提供的安全性:

const result = sum(youTubeVideos, (item) => {
 // Parameter 'item' implicitly has an 'any' type.
  // We can return anything from here, not just
  // a number!
  return item.name;
});

TypeScript 现在无法推断应该是什么 item ,或者我们的mapper函数应该返回什么。

这里的教训是“不要使用 Function ” - 总有一个更具体的选项可用。

4、表示“任何函数”

有时,期望在typescript中表示“任何函数”,为此,让我们看一下 TypeScript 的一些内置类型Parameters 以及 ReturnType。

export type Parameters<
  T extends (...args: any) => any
> = T extends (...args: infer P) => any
  ? P
  : never;
 
export type ReturnType<
  T extends (...args: any) => any
> = T extends (...args: any) => infer R ? R : any;

您会注意到这两种实用程序类型使用相同的约束:(...args: any) => any 。

(...args: any)指定函数可以接受任意数量的参数,=> any 表示它可以返回任何内容。

5、表示没有参数的函数

要表达一个没有参数的函数(但返回任何内容),您需要使用() => any:

const wrapFuncWithNoArgs = (func: () => any) => {
  try {
    return func();
  } catch (e) {}
};
 
wrapFuncWithNoArgs((a: string) => {});

> Argument of type '(a: string) => void' is not assignable to parameter of type '() => any'.
> Target signature provides too few arguments. Expected 1 or more, but got 0.

6、总结

Function不应该用作表示函数类型。当您只想指定参数而不指定返回类型时,可以使用语法(a: string, b: number) => any。

记住,(...args: any) => any 可用于表示任何函数类型。

此处,mapper 表示从对象中提取数字的函数。该 sum 函数的强大之处在于您可以丢弃大多数此类声明:

const youTubeVideos = [
  { name: "My favorite cheese", views: 100 },
  {
    name: "My second favorite cheese (you won't believe it)",
    views: 67,
  },
];
 
const result = sum(youTubeVideos, (video) => {
  return video.views;
}); // 167

事实上,我们已经舍弃了所有类型声明,但 video仍旧被推断为 { name: string; views: number } 。这是可能的,因为我们的函数定义的特殊性:(item: T) => number 。

责任编辑:武晓燕 来源: 宇宙一码平川
相关推荐

2023-11-30 09:00:00

TypeScript开发

2023-08-31 09:10:18

JavaScript调试

2021-03-29 08:05:15

User项目安全

2019-04-15 14:05:56

MySQLUTF-8数据库

2022-08-10 14:36:05

Python循环函数

2018-07-16 16:29:26

编程函数代码

2023-07-04 15:11:30

TypeScript类型保护

2020-06-04 08:10:30

Python字符串开发

2021-10-18 10:53:26

Go 代码技术

2018-06-25 14:29:45

MySQLbug数据库

2020-06-23 14:52:04

Python无用分号语言

2022-05-17 08:25:10

TypeScript接口前端

2021-04-26 09:33:46

Go Iota语言

2022-05-10 09:12:16

TypeScript装饰器

2023-01-05 17:13:28

TypeScript泛型组件

2020-12-01 11:18:34

对外接口枚举

2015-10-10 10:36:00

warning category

2022-11-18 14:58:34

JavaScript语言TypeScript

2023-10-18 16:30:50

2023-04-13 11:05:10

5G网络无线技术
点赞
收藏

51CTO技术栈公众号