1. 程式人生 > 其它 >TypeScript 定義函式的幾種寫法

TypeScript 定義函式的幾種寫法

寫法1 - 使用 function 關鍵字

function greeter(fn: (a: string) => void) {
  fn("Hello, World");
}

function printToConsole(s: string) {
  console.log(s);
}

greeter(printToConsole);

(a: string) => void

上述語法的含義:表示一個函式,接收一個字串作為輸入引數,沒有返回引數。

可以使用 type 關鍵字定義一個別名:

type GreetFunction = (a: string) => void;

Call signatures

使用 call signatures 給函式增加額外的屬性。TypeScript 的 function 也是 value,和其他 value 一樣。

注意:一定有 type 關鍵字。

原始碼:

type DescribableFunction = {
    description: string;
    (someArg: number): boolean;
  };
  
function doSomething(fn: DescribableFunction) {
    console.log(fn.description + " returned " + fn(6));
}

const fn = (a: number) => a > 10;

fn.description = 'Jerry';

另一種定義方式:

type DescribableFunction = {
    description: string;
    (someArg: number): boolean;
  };
const fn = <DescribableFunction>({
   description: 'Jerry'
});

const fn23 = Object.assign(
  function (number:number) { return number > 1 },
  fn
);
function doSomething(fn: DescribableFunction) {
    console.log(fn.description + " returned " + fn(6));
}

Construct signatures

type SomeConstructor = {
  new (s: string): SomeObject;
};
function fn(ctor: SomeConstructor) {
  return new ctor("hello");
}

Method Signatures

方法簽名語法可能是最容易使用的。 在定義物件型別時,可以通過提供如下簽名來輕鬆描述其方法:

interface Date {
  toString(): string;
  setTime(time: number): number;
  // ...
}

看個例子:

通過這種方式定義出來的函式,實際是一個 object:

如何例項化這種型別的 object?

const myDate = <MyDate>({
  toString: () => 'Jerry',
  setTime: (number) => number + 1
});

Function Type Literals

Function Type Literals 是另一種宣告函式型別的方法。 它們通常用於高階函式的簽名,即接受函式作為引數或返回函式的函式:

interface Array<T> {
  sort(compareFn?: (a: T, b: T) => number): this;
  // ...
}

也許令人驚訝的是,在函式型別文字中總是需要引數名稱。 您不能省略引數名稱而只指定型別。

如果忘記指定形參名稱,我們將無法得到期望的型別定義。下面的程式碼是一個例子:

我們本意想定義一個擁有兩個輸入引數,一個返回引數的函式型別,輸入引數型別分別為 string 和 number.

type FunctionType2 = (string, number) => number;
// (string: any, number: any) => number

實際上,TypeScript 編譯器將 string 和 number 理解成了形式引數名,且型別為 any. 這就和我們的期望不一致了。

總結一下:

Object Type Literals with Call or Construct Signatures

在 JavaScript 中,函式只不過是可以呼叫的特殊物件。 這個事實反映在物件型別文字的語法中:它們描述了物件的形狀,它也恰好有一個呼叫簽名:

interface RegExpConstructor {
  // Call signatures
  (pattern: RegExp): RegExp;
  (pattern: string, flags?: string): RegExp;

  // ...
}

與呼叫簽名類似,物件型別文字也可以包含構造簽名,在這種情況下,它被稱為建構函式型別。 當使用 new 運算子呼叫函式時,函式的構造簽名定義了它的引數列表和返回型別。 構造簽名看起來與呼叫簽名幾乎相同,除了它們額外帶有 new 關鍵字字首:

interface RegExpConstructor {
  // Call signatures
  (pattern: RegExp): RegExp;
  (pattern: string, flags?: string): RegExp;

  // Construct signatures
  new (pattern: RegExp): RegExp;
  new (pattern: string, flags?: string): RegExp;

  // ...
}
// Using the call signature
const digitsPattern1 = RegExp("^\\d+$");

// Using the construct signature
const digitsPattern2 = new RegExp("^\\d+$");

更多Jerry的原創文章,盡在:"汪子熙":