Naming complex public types
Concept and boundary
Give a meaningful exported name to a repeated public contract. Named types can improve declaration readability and sometimes allow reuse in checker work; measure rather than assume a performance gain. Inspect the generated declaration to verify what consumers actually see.
Accepted project
Create a fresh folder with exactly these files. Use TypeScript 5.8.3 and Node.js 22.16.0. Package stubs under node_modules are local teaching fixtures, not installed dependencies.
export interface Report{rows:readonly {id:number;label:string}[]} export function make():Report{return {rows:[]};}tsconfig.json
{ "compilerOptions": { "strict": true, "target": "ES2022", "module": "CommonJS", "moduleResolution": "Node10", "moduleDetection": "force", "skipLibCheck": true, "noEmitOnError": true, "types": [], "declaration": true, "extendedDiagnostics": true }, "include": [ "**/*" ], "exclude": [ "node_modules", "dist", "build", "cache" ]}From the project root run: tsc -p tsconfig.json --pretty false.
Compilation succeeds (exit 0).
No runtime execution is required for this check.
main.d.ts exists.
Contains "interface Report".
Contains "make(): Report".
Compiler output contains "Check time:".
Rejected project — diagnosis
Intentionally rejected variant. Do not execute it.
Use a fresh copy of the accepted project, changing only the files shown below.
export interface Report{rows:readonly {id:number;label:string}[]} export function make():Report{return {rows:[{id:"1",label:"x"}]};}From the project root run: tsc -p tsconfig.json --pretty false.
Expected compiler diagnostic: TS2322
No runtime execution is required for this check.
The named public contract still requires numeric IDs. Restore the accepted configuration/source to compare the result.
Independent comparison: anonymous-return
An anonymous annotation is emitted inline for this function. Compare the declaration and measured check time; the smaller sample does not establish a performance rule.
Create a fresh folder with exactly these files. Use TypeScript 5.8.3 and Node.js 22.16.0. Package stubs under node_modules are local teaching fixtures, not installed dependencies.
export function make():{rows:readonly {id:number;label:string}[]}{return {rows:[]};}tsconfig.json
{ "compilerOptions": { "strict": true, "target": "ES2022", "module": "CommonJS", "moduleResolution": "Node10", "moduleDetection": "force", "skipLibCheck": true, "noEmitOnError": true, "types": [], "declaration": true, "extendedDiagnostics": true }, "include": [ "**/*" ], "exclude": [ "node_modules", "dist", "build", "cache" ]}From the project root run: tsc -p tsconfig.json --pretty false.
Compilation succeeds (exit 0).
No runtime execution is required for this check.
main.d.ts exists.
Contains "make(): {".
Does not contain "interface Report".
Compiler output contains "Check time:".
Practice and recap
Inspect main.d.ts and compare an anonymous return annotation.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Naming complex public types — knowledge check
Check the concept, interpret the tested project result, and diagnose a rejected configuration or source.
Which statement correctly describes Naming complex public types?
Using the complete accepted project and the exact command in the lesson, which result is expected?
export interface Report{rows:readonly {id:number;label:string}[]} export function make():Report{return {rows:[]};}Why does the rejected project fail under its stated configuration?
export interface Report{rows:readonly {id:number;label:string}[]} export function make():Report{return {rows:[{id:"1",label:"x"}]};}What is the result of the independent anonymous-return comparison?
export function make():{rows:readonly {id:number;label:string}[]}{return {rows:[]};}