Function signature inference
Concept and syntax
A conditional function pattern can capture the argument tuple and return type together. This models a signature, not a function call. The extracted arguments preserve positional types.
Working example
Save each snippet separately as main.ts in a fresh directory with TypeScript 5.8.3. Compile with tsc --strict --target ES2022 --module commonjs --moduleDetection force --skipLibCheck --noEmitOnError main.ts and run accepted snippets with node main.js. Type aliases are erased; assignments and diagnostic checks demonstrate the type relations, not the printed output alone.
type Parts<T>=T extends (...args:infer A)=>infer R?[A,R]:never; type P=Parts<(name:string,count:number)=>boolean>;const p:P=[["tea",2],true]; console.log(JSON.stringify(p));Expected output:
[["tea",2],true]Common mistake
Intentionally invalid; compile separately and do not execute.
type Parts<T>=T extends (...args:infer A)=>infer R?[A,R]:never; type P=Parts<(name:string,count:number)=>boolean>;const p:P=[[2,"tea"],true];Expected diagnostic: TS2322. The extracted parameter tuple starts with string then number.
Practice and recap
Extract a signature with an optional second argument.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Function signature inference — knowledge check
Check the type transformation, trace output, diagnose a rejected example, and apply the concept to a new case.
Which statement about Function signature inference is accurate?
What is the complete stdout, represented as a JSON string (including newline characters)?
type Parts<T>=T extends (...args:infer A)=>infer R?[A,R]:never; type P=Parts<(name:string,count:number)=>boolean>;const p:P=[["tea",2],true]; console.log(JSON.stringify(p));Which diagnostic and explanation match the rejected example under the lesson settings?
type Parts<T>=T extends (...args:infer A)=>infer R?[A,R]:never; type P=Parts<(name:string,count:number)=>boolean>;const p:P=[[2,"tea"],true];What is Parts<() => number>?