Home > Enterprise >  what typescript type would an anonymous function be?
what typescript type would an anonymous function be?

Time:03-01

thanks i'm learning to declare functions in .d.ts files

my javascript has

function generate(romeo) {
  return function(juliet) {
    return romeo   juliet 
  }
}

typescript declaration

  /**
   * Function that accepts strings and returns composed function.
   */
  export function generate(romeo: string): (juliet: string) => string;

object?

  • found an object type about being non primitive
  • didn't find any function type
  • What types would be legit?

CodePudding user response:

As type alias

type Fn = (s1: string) => (s2: string) => string

This is probably the nicest way to read it: Function that takes a string s1 and returns another function which takes a string s1. The second function returns a string.

The advantage here is that the function notation is consistent.

Can also be written with extra grouping brackets if more clarity is needed: type Fn = (s1: string) => ((s2: string) => string)

As interface

interface Fn {
    (s1: string): (s2: string) => string
}

It means the same thing as above. It is (at least in my eyes) more confusing to read because of the mix of : and => to denote return values.

As part of the definition of the function

function generate(romeo: string): (juliet: string) => string {
  return function(juliet: string): string {
    return romeo   juliet 
  }
}

If you do not really need a separate type for it. Note that the names of the parameter in the type annotation and the actual function definition do not need to match. The following is equivalent:

function generate(romeo: string): (s: string) => string {
  return function(juliet: string): string {
    return romeo   juliet 
  }
}

If you do not have the explicit-function-return-types linter rule from TypeScript ESLint (or equivalent), then you can let TypeScript infer the types:

function generate(romeo: string) {
  return function(juliet: string) {
    return romeo   juliet 
  }
}

CodePudding user response:

As VLAZ points out in the comments, the type Function exists, but it's not very useful: The point of TypeScript is to have certainty about the types that are returned, and Function does not indicate its parameters or return value.

As in the docs on function type expressions, you can define a function type like this:

type FunctionType = (param1: SomeParameter) => SomeReturnValue;

For your case, that would look like this:

/**
 * Function that accepts strings and returns composed function.
 */
export function generate(romeo: string): (juliet: string) => string;

You'll need to specify some kind of return value in order for TypeScript to recognize this as a function type, which could be any or unknown but ideally will be a more specific type like string above. (As usual, if the function is intended to not have a useful return value, then void is appropriate.) There is also a way to specify a Function that has additional properties, which is described in the docs as call signatures.

  • Related