• Reference link 1
  • Reference link 2

Write 1 – Use the function keyword

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

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

greeter(printToConsole);

(a: string) => void

Represents a function that accepts a string as input and returns no arguments.

You can define an alias using the type keyword:

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

Call signatures

Use call signatures to add additional properties to the function. TypeScript functions are values, just like any other value.

Note: there must be the type keyword.

The source code:

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';

Another way to define it:

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

The method signature syntax is probably the easiest to use. When defining an object type, you can easily describe its methods by providing the following signature:

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

Here’s an example:

The function defined in this way is actually an object:

How do I instantiate an Object of this type?

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

Function Type Literals

Function Type Literals is another way to declare the Type of a Function. They are typically used for signatures of higher-order functions, that is, functions that take functions as arguments or return functions:

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

Perhaps surprisingly, parameter names are always required in function-type literals. You cannot omit the parameter name and just specify the type.

If we forget to specify the parameter name, we will not get the desired type definition. The following code is an example:

We intended to define a function type that has two input arguments and a return argument of type String and Number.

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

In fact, the TypeScript compiler interprets String and Number as formal parameter names of type Any. This is not consistent with our expectations.

To summarize:

Object Type Literals with Call or Construct Signatures

In JavaScript, functions are nothing more than special objects that can be called. This fact is reflected in the syntax of the object type literals: they describe the shape of the object, and it also happens to have a call signature:

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

Similar to the call signature, an object type literal can also contain a construct signature, in which case it is referred to as a constructor type. When a function is called with the new operator, the function’s construction signature defines its argument list and return type. Constructing signatures look almost the same as calling signatures, except that they are prefixed with the new keyword:

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+$");

More of Jerry’s original articles can be found on “Wang Zixi “: