Call and construct signatures
Concept and syntax
An object type can declare a call signature (x: number): number or a construct signature new (x: number): Item. The first describes a callable value; the second describes a value usable with new. The constructor value and the constructed instance are different types.
Working example
Save each snippet separately as main.ts in a fresh directory. Compile with tsc --strict --target ES2022 --module commonjs --moduleDetection force --skipLibCheck --noEmitOnError main.ts, then run the accepted example with node main.js. Forced module detection isolates snippet names from global declarations.
type Calc={ (n:number):number };type Maker={new(n:number):{value:number}};const double:Calc=n=>n*2;const Box:Maker=class {constructor(public value:number){}};console.log(double(3),new Box(8).value);Expected output:
6 8Common mistake
Intentionally invalid; compile separately and do not execute.
type Maker={new(n:number):{value:number}};const make:Maker=(n:number)=>({value:n});Expected diagnostic: TS2322. An arrow function is callable but not constructible. Use a class or a suitable constructor.
Practice and recap
Define a callable interface accepting a string and returning its length.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Call and construct signatures — knowledge check
Check the contract, trace output, and diagnose an invalid example.
Which statement about this lesson’s concept is accurate?
What is the complete stdout, represented as a JSON string (including newline characters)?
type Calc={ (n:number):number };type Maker={new(n:number):{value:number}};const double:Calc=n=>n*2;const Box:Maker=class {constructor(public value:number){}};console.log(double(3),new Box(8).value);Which diagnostic and explanation match the rejected example under the lesson settings?
type Maker={new(n:number):{value:number}};const make:Maker=(n:number)=>({value:n});