Type and value namespaces
Concept and syntax
Some declarations exist only in the type namespace, such as interfaces and type aliases. Variables exist in the value namespace. Classes introduce both a value constructor and an instance type. A type-only name is erased and cannot be used as a runtime constructor.
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.
interface Item {count:number;}const Item={count:7};const x:Item=Item;class Meter {count=2;}const m:Meter=new Meter();console.log(x.count,m.count);Expected output:
7 2Common mistake
Intentionally invalid; compile separately and do not execute.
Expected diagnostic: TS2693. An interface has no runtime constructor. Create an object literal satisfying it or use a class.
Practice and recap
Declare a type alias and a value of the same name; use both positions.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Type and value namespaces — 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)?
interface Item {count:number;}const Item={count:7};const x:Item=Item;class Meter {count=2;}const m:Meter=new Meter();console.log(x.count,m.count);Which diagnostic and explanation match the rejected example under the lesson settings?
interface Item {count:number;}const x=new Item();