自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

慎用!??!不要在Typescript中使用Function類型

開發(fā) 前端
事實上,我們已經(jīng)舍棄了所有類型聲明,但 video?仍舊被推斷為 { name: string; views: number } ?。這是可能的,因為我們的函數(shù)定義的特殊性:(item: T) => number 。

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

翻譯:一川

在Typescript中不應(yīng)該使用Function作為一個類型,因為它可以表示任何函數(shù)。通常,我們期望的是更具體的類型--例如指定參數(shù)的數(shù)量或函數(shù)返回的內(nèi)容。如果確實要表示可以接受任意數(shù)量的參數(shù)并返回任何類型的函數(shù),請使用 (...args: any[]) => any。

1、完整的解釋

假設(shè)您正在創(chuàng)建一個匯總對象數(shù)組的函數(shù)。這是一個,取自Excalidraw代碼庫:

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

讓我們看一下類型定義。此函數(shù)包含:

  • 只讀數(shù)組:readonly T[]
  • 映射器函數(shù):(item: T) => number

并返回 number類型。

在主體中,它調(diào)用array.reduce(func, 0)。這意味著在acc

對于數(shù)組的每個成員,它通過將acc和mapper(item)進行相加,最終得到數(shù)組所有成員的總和。

2、什么可以用來作為函數(shù)聲明?

mapper函數(shù)是關(guān)鍵。我們來剝離一下來看看:

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、關(guān)于什么是函數(shù)?

我看到很多初學(xué)者開發(fā)人員犯的一個大錯誤,聲明一個類似于 mapper和Function類型 的函數(shù):

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

這個關(guān)鍵字基本上代表“任何函數(shù)”。這意味著從技術(shù)上講可以 sum 接收任何函數(shù)。

當(dāng)在 中使用 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 現(xiàn)在無法推斷應(yīng)該是什么 item ,或者我們的mapper函數(shù)應(yīng)該返回什么。

這里的教訓(xùn)是“不要使用 Function ” - 總有一個更具體的選項可用。

4、表示“任何函數(shù)”

有時,期望在typescript中表示“任何函數(shù)”,為此,讓我們看一下 TypeScript 的一些內(nèi)置類型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)指定函數(shù)可以接受任意數(shù)量的參數(shù),=> any 表示它可以返回任何內(nèi)容。

5、表示沒有參數(shù)的函數(shù)

要表達一個沒有參數(shù)的函數(shù)(但返回任何內(nèi)容),您需要使用() => 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、總結(jié)

Function不應(yīng)該用作表示函數(shù)類型。當(dāng)您只想指定參數(shù)而不指定返回類型時,可以使用語法(a: string, b: number) => any。

記住,(...args: any) => any 可用于表示任何函數(shù)類型。

此處,mapper 表示從對象中提取數(shù)字的函數(shù)。該 sum 函數(shù)的強大之處在于您可以丟棄大多數(shù)此類聲明:

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

事實上,我們已經(jīng)舍棄了所有類型聲明,但 video仍舊被推斷為 { name: string; views: number } 。這是可能的,因為我們的函數(shù)定義的特殊性:(item: T) => number 。

責(zé)任編輯:武曉燕 來源: 宇宙一碼平川
相關(guān)推薦

2023-11-30 09:00:00

TypeScript開發(fā)

2023-08-31 09:10:18

JavaScript調(diào)試

2019-04-15 14:05:56

MySQLUTF-8數(shù)據(jù)庫

2021-03-29 08:05:15

User項目安全

2022-08-10 14:36:05

Python循環(huán)函數(shù)

2018-07-16 16:29:26

編程函數(shù)代碼

2023-07-04 15:11:30

TypeScript類型保護

2020-06-04 08:10:30

Python字符串開發(fā)

2021-10-18 10:53:26

Go 代碼技術(shù)

2018-06-25 14:29:45

MySQLbug數(shù)據(jù)庫

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網(wǎng)絡(luò)無線技術(shù)
點贊
收藏

51CTO技術(shù)棧公眾號