Higher-kinded encodings
Concept and syntax
TypeScript does not expose first-class higher-kinded type parameters. A finite URI-to-type encoding can approximate application of a type constructor: use a registry indexed by a tag and a value type. It supports registered constructors, not arbitrary generic type functions.
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.
interface Registry<A>{list:A[];box:{value:A}} type Apply<F extends keyof Registry<unknown>,A>=Registry<A>[F];const b:Apply<"box",number>={value:4}; const l:Apply<"list",string>=["tea"]; console.log(b.value,l[0]);Expected output:
4 teaCommon mistake
Intentionally invalid; compile separately and do not execute.
interface Registry<A>{list:A[];box:{value:A}} type Apply<F extends keyof Registry<unknown>,A>=Registry<A>[F];const b:Apply<"box",number>={value:"four"};Expected diagnostic: TS2322. The selected box constructor preserves the numeric argument.
Practice and recap
Register an optional wrapper and test Apply for it.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Higher-kinded encodings — knowledge check
Check the type transformation, trace output, diagnose a rejected example, and apply the concept to a new case.
Which statement about Higher-kinded encodings is accurate?
What is the complete stdout, represented as a JSON string (including newline characters)?
interface Registry<A>{list:A[];box:{value:A}} type Apply<F extends keyof Registry<unknown>,A>=Registry<A>[F];const b:Apply<"box",number>={value:4}; const l:Apply<"list",string>=["tea"]; console.log(b.value,l[0]);Which diagnostic and explanation match the rejected example under the lesson settings?
interface Registry<A>{list:A[];box:{value:A}} type Apply<F extends keyof Registry<unknown>,A>=Registry<A>[F];const b:Apply<"box",number>={value:"four"};What is Apply<"list", boolean>?