Skip to main content

typescript

PropertyValue
Packagetypescript
Versions Covered>=4.0.0 <6.0.0
Contract Version1.0.0
Statusproduction
Last Verified2026-02-24
Maintainercorpus-team

Installation​

npm install typescript

Covered Functions​

This contract covers 5 function(s):

readFile()​

Reads a file from the file system synchronously and returns its contents as a string

Import:

import { readFile } from 'typescript';

Postconditions​

What happens after calling this function:

🔴 ERROR - file-not-found

Condition: file does not exist at the specified path

Throws: Error with code ENOENT (file not found)

Required Handling:

Caller MUST wrap ts.sys.readFile() in a try-catch block to handle file-not-found errors. Uncaught ENOENT errors will crash the application.

📖 Source

🔴 ERROR - permission-denied

Condition: insufficient permissions to read the file

Throws: Error with code EACCES (permission denied)

Required Handling:

Caller MUST wrap ts.sys.readFile() in a try-catch block to handle permission errors. File system permissions can vary across environments.

📖 Source

âš ī¸ WARNING - encoding-error

Condition: file contains invalid encoding or binary data read as text

Throws: Error or returns corrupted string data

Required Handling:

Caller SHOULD validate file contents after reading, especially when reading user-provided or external files. Binary files read as text will produce invalid results.

📖 Source

Edge Cases​

Known gotchas and sharp edges:

â„šī¸ INFO - undefined-return

May return undefined instead of throwing if file doesn't exist (implementation-dependent)

📖 Source


readDirectory()​

Reads a directory and returns an array of file paths matching the specified extensions

Import:

import { readDirectory } from 'typescript';

Postconditions​

What happens after calling this function:

🔴 ERROR - directory-not-found

Condition: directory does not exist at the specified path

Throws: Error with code ENOENT (directory not found)

Required Handling:

Caller MUST wrap ts.sys.readDirectory() in a try-catch block to handle directory-not-found errors. Uncaught ENOENT errors will crash the application.

📖 Source

🔴 ERROR - permission-denied

Condition: insufficient permissions to read the directory

Throws: Error with code EACCES (permission denied)

Required Handling:

Caller MUST wrap ts.sys.readDirectory() in a try-catch block to handle permission errors. Directory permissions can vary across environments.

📖 Source

âš ī¸ WARNING - symlink-loop

Condition: directory contains circular symbolic links

Throws: Error with code ELOOP (too many symbolic links)

Required Handling:

Caller SHOULD wrap ts.sys.readDirectory() in a try-catch to handle symlink loops, especially when scanning user-provided or external directories.

📖 Source

Edge Cases​

Known gotchas and sharp edges:

â„šī¸ INFO - empty-array-return

Returns empty array if no files match the specified extensions, not an error

📖 Source


writeFile()​

Writes content to a file synchronously

Import:

import { writeFile } from 'typescript';

Postconditions​

What happens after calling this function:

🔴 ERROR - permission-denied

Condition: insufficient permissions to write to the file or directory

Throws: Error with code EACCES (permission denied)

Required Handling:

Caller MUST wrap ts.sys.writeFile() in a try-catch block to handle permission errors. Write permissions can be restricted in production environments.

📖 Source

🔴 ERROR - directory-not-found

Condition: parent directory does not exist

Throws: Error with code ENOENT (directory not found)

Required Handling:

Caller MUST either: 1. Wrap ts.sys.writeFile() in a try-catch and handle ENOENT, OR 2. Create parent directories before writing (e.g., using ts.sys.createDirectory or mkdirp)

📖 Source

âš ī¸ WARNING - disk-full

Condition: insufficient disk space

Throws: Error with code ENOSPC (no space left on device)

Required Handling:

Caller SHOULD wrap ts.sys.writeFile() in a try-catch to handle disk-full errors, especially for build tools that may write large files.

📖 Source

âš ī¸ WARNING - read-only-filesystem

Condition: filesystem is mounted read-only

Throws: Error with code EROFS (read-only file system)

Required Handling:

Caller SHOULD wrap ts.sys.writeFile() in a try-catch to handle read-only filesystem errors.

📖 Source


createProgram()​

Creates a TypeScript program from source files and compiler options

Import:

import { createProgram } from 'typescript';

Postconditions​

What happens after calling this function:

âš ī¸ WARNING - file-not-found

Condition: source file specified in rootNames does not exist

Throws: May throw Error or return program with diagnostics

Required Handling:

Caller SHOULD wrap ts.createProgram() in a try-catch AND check ts.getPreEmitDiagnostics() for file-not-found errors. The API may return a program with errors rather than throwing.

📖 Source

âš ī¸ WARNING - invalid-compiler-options

Condition: compiler options are invalid or incompatible

Throws: May throw Error or return program with diagnostics

Required Handling:

Caller SHOULD validate compiler options before passing to ts.createProgram() or check diagnostics after program creation.

📖 Source

âš ī¸ WARNING - unchecked-diagnostics

Condition: program created successfully but contains compilation errors

Returns:

Program object with errors accessible via getPreEmitDiagnostics()

Required Handling:

Caller MUST call ts.getPreEmitDiagnostics(program) after creating a program to check for compilation errors. Programs can be created even when source files have errors, but emitting without checking diagnostics is unsafe.

📖 Source

Edge Cases​

Known gotchas and sharp edges:

â„šī¸ INFO - module-resolution-failure

If module resolution fails, errors appear in diagnostics rather than throwing

📖 Source


createCompilerHost()​

Creates a compiler host that provides file system and environment operations

Import:

import { createCompilerHost } from 'typescript';

Postconditions​

What happens after calling this function:

âš ī¸ WARNING - custom-host-file-errors

Condition: custom getSourceFile implementation encounters file system errors

Throws: Depends on custom implementation - may throw or return undefined

Required Handling:

When implementing custom CompilerHost, the getSourceFile method MUST handle file system errors (ENOENT, EACCES) gracefully. Either wrap fs operations in try-catch and return undefined, or allow errors to propagate with clear messages.

📖 Source

Edge Cases​

Known gotchas and sharp edges:

â„šī¸ INFO - default-host-delegates-to-sys

Default compiler host delegates to ts.sys, which can throw file system errors

📖 Source


Example: Proper Error Handling​

import typescript from 'typescript';

async function example() {
try {
const result = await readFile(/* args */);
// Handle success
return result;
} catch (error) {
// Handle error according to contract postconditions
console.error('Error:', error);
throw error;
}
}

See Also​