Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

TypeScript, what are call signature of an object literal and how can they be used with generic types?

Tags:

I'm reading this section of the TypeScript documentation, under the generic types section, the following two are stated to be the equivalent:

Code Sample 1

function identity<T>(arg: T): T {     return arg; }  let myIdentity: <T>(arg: T) => T = identity; 

Code Sample 2

function identity<T>(arg: T): T {     return arg; }  let myIdentity: {<T>(arg: T): T} = identity; 

The documentation states this is possible due to the following.

We can also write the generic type as a call signature of an object literal type

Despite this line I'm still struggling to understand how the two are equivalent, is there any further documentation or explanation of what it means to be 'a call signature of an object literal type'.

I'm sorry I can't give any further explanation but I'm completely drawing a blank as to how the two are equivalent, to me the second type definition states that myIdentity should be an object?

Thanks.

like image 894
tomhughes Avatar asked Feb 24 '18 20:02

tomhughes


People also ask

What is a call signature in TypeScript?

In TypeScript we can express its type as: ( a : number , b : number ) => number. This is TypeScript's syntax for a function's type, or call signature (also called a type signature). You'll notice it looks remarkably similar to an arrow function—this is intentional!

What is object literal in TypeScript?

They are a comma-separated list of name-value pairs. Think of JSON Objects. Below is an Object Literal which contains the information for a user. An example of an Object Literal. Let's look at different ways to declare object literals in TypeScript and some caveats associated with them.

What is a generic type in TypeScript?

Generics offer a way to create reusable components. Generics provide a way to make components work with any data type and not restrict to one data type. So, components can be called or used with a variety of data types. Generics in TypeScript is almost similar to C# generics.

What is type signature TypeScript?

Today I will describe how to write type signatures for a wide range of javascript functions. This will help readers narrow types, increase reliability, type dependencies, and more. A type signature is like a blueprint for any given segment of code.


2 Answers

Functions can have properties, that's what the object literal syntax is for: it allows to define a call signature and additional properties. Your two examples are equivalent because the second doesn't define additional properties on the object literal. You can read more on that in the section on hybrid types.

Additionally, the object literal allows to define multiple call signatures for function overloads. You can create an object of such an interface with Object.assign:

interface Foo {     (x: string): number,     (x: number): string,     bar: Array<any>, }  const foo: Foo = Object.assign(function (x: any) {     if (typeof x === 'string') {         return parseInt(x);     } else {         return x.toString();     } }, {     bar: [] }); 
like image 79
Tao Avatar answered Oct 05 '22 10:10

Tao


It is because Function in JavaScript is also an object.

Consider the following:

function foo() { return 'foo' }  // vs  const foo = Object.assign(   function () { return 'foo' },   {} ) 

TypeScript just follows what is possible in JavaScript.

like image 36
unional Avatar answered Oct 05 '22 12:10

unional