Summary - Emit
- TypeScript course
- Expert
- Lesson 22/35
- 4 min
Write a library build configuration with emit options.
Summary: library build config
A library build config combines four emit parts: outDir sets the output directory, declaration: true produces .d.ts, sourceMap: true adds a debug map, and when needed emitDecoratorMetadata: true carries decorator metadata to runtime. Together they produce a library that is consumable and debuggable.
{
"compilerOptions": {
"outDir": "dist",
"declaration": true,
"sourceMap": true,
"emitDecoratorMetadata": true,
"experimentalDecorators": true
}
}These settings are the skeleton of libraries published to npm.
Practice Validate the library config
Write a validateLibraryConfig function that checks a config object's declaration and outDir fields as required, and sourceMap and emitDecoratorMetadata as optional. Push to an errors array for missing required fields. Print the error count for the config { declaration: true, outDir: "dist", sourceMap: true }.
Starter code
// validateLibraryConfig: kütüphane config'ini doğrular
// declaration ve outDir zorunlu
// sourceMap ve emitDecoratorMetadata isteğe bağlı
// eksik zorunlu alan için hata ekle
// hata sayısını yazdırSample solution
type LibraryConfig = {
declaration?: boolean;
outDir?: string;
sourceMap?: boolean;
emitDecoratorMetadata?: boolean;
};
function validateLibraryConfig(config: LibraryConfig): string[] {
const errors: string[] = [];
if (!config.declaration) errors.push("declaration must be true");
if (!config.outDir) errors.push("outDir must be set");
return errors;
}
const config = { declaration: true, outDir: "dist", sourceMap: true };
console.log(validateLibraryConfig(config).length);Expected output
0
Questions and answers
Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.
1 Which options does a library config include?
- Only noEmit and inlineSourceMap
- Only emitDeclarationOnly and rootDir
- Only outDir and noEmit
- outDir, declaration, sourceMap, and emitDecoratorMetadata when needed Correct answer
Explanation: A library config combines output, declaration, source map, and when needed decorator metadata; noEmit produces no output so it is not suited for a library.
2 Produce declarations for the library
{
"compilerOptions": {
"outDir": "dist",
"____": true
}
}
{
"compilerOptions": {
"outDir": "dist",
"declaration": true
}
}
Explanation: To produce declarations for a library use declaration; noEmit produces no output, emitDeclarationOnly produces only .d.ts, and sourceMap is the map.
3 Library config parts
| Term | Meaning |
|---|---|
outDir |
sets the output directory |
declaration: true |
produces .d.ts |
sourceMap: true |
debug map |
emitDecoratorMetadata: true |
carries decorator type info |