Function parameter compatibility
Concept and syntax
A function that ignores extra arguments can be used where a caller supplies them. Return values must be compatible with the target return type, and under strictFunctionTypes ordinary function parameter positions are checked contravariantly. A target must not promise calls the source cannot handle.
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.
const first=(n:number)=>n+1;const cb:(n:number,label:string)=>number=first;console.log(cb(4,"x"));Expected output:
5Common mistake
Intentionally invalid; compile separately and do not execute.
const requiresTwo=(a:number,b:number)=>a+b;const cb:(a:number)=>number=requiresTwo;Expected diagnostic: TS2322. The target allows callers to omit b. The source requires it; make the source safe for such calls.
Practice and recap
Assign a function returning a literal number to a function returning number.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Function parameter compatibility — 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)?
const first=(n:number)=>n+1;const cb:(n:number,label:string)=>number=first;console.log(cb(4,"x"));Which diagnostic and explanation match the rejected example under the lesson settings?
const requiresTwo=(a:number,b:number)=>a+b;const cb:(a:number)=>number=requiresTwo;