Skip to main content
The types command runs TypeScript’s type checker on your Native Desktop project to validate type safety, catch type errors, and ensure your code adheres to TypeScript’s type system without emitting any output files.

Prerequisites

Before running the types command, ensure:
  • Your project has been created using native-desktop create
  • All dependencies are installed (npm install)
  • TypeScript configuration exists (tsconfig.json)
  • Your project uses TypeScript (.ts or .tsx files)

Basic Usage

To check TypeScript types in your project, open the terminal in the root directory of your Native Desktop project and run:
native-desktop types
This command will:
  1. Load TypeScript configuration from tsconfig.json
  2. Analyze all TypeScript files in your project
  3. Check for type errors and inconsistencies
  4. Report any type-related issues
  5. Exit with an appropriate status code

What Gets Checked

By default, the types command checks:
  • All TypeScript files (.ts, .tsx)
  • Type definitions (.d.ts files)
  • Files in the src/ directory
  • Files specified in tsconfig.json include patterns
Files and directories specified in tsconfig.json exclude patterns are automatically skipped.

Output Examples

Successful Type Check (No Errors)

$ native-desktop types

 Type checking complete
 No type errors found
All TypeScript types are valid

With Type Errors

$ native-desktop types

 Type checking failed: 3 errors found

src/components/Button.tsx:15:7
  Type 'string' is not assignable to type 'number'.
  15   const count: number = "hello";
           ~~~~~

src/utils/helpers.ts:23:16
  Property 'name' does not exist on type 'User'.
  23   return user.name;
                  ~~~~

src/index.ts:45:10
  Argument of type 'boolean' is not assignable to parameter of type 'string'.
  45   myFunction(true);
          ~~~~

 Found 3 type errors

With Warnings

$ native-desktop types

 Type checking complete with warnings

src/models/User.ts:12:5
  'age' is declared but its value is never read.
  12   age: number;
       ~~~

 0 errors, 1 warning

TypeScript Configuration

The types command uses the tsconfig.json file generated by native-desktop create. Here’s an example configuration:
{
  "compilerOptions": {
    "target": "ES2022",
    "module": "ESNext",
    "lib": ["ES2022"],
    "moduleResolution": "node",
    "strict": true,
    "esModuleInterop": true,
    "skipLibCheck": true,
    "forceConsistentCasingInFileNames": true,
    "resolveJsonModule": true,
    "noEmit": true,
    "noUnusedLocals": true,
    "noUnusedParameters": true,
    "noImplicitReturns": true,
    "noFallthroughCasesInSwitch": true
  },
  "include": ["src/**/*"],
  "exclude": ["node_modules", "bin", "dist"]
}

TypeScript Compiler Options

Common compiler options that affect type checking:
  • strict: Enable all strict type-checking options
  • noImplicitAny: Error on expressions with implied any type
  • strictNullChecks: Enable strict null checking
  • strictFunctionTypes: Enable strict checking of function types
  • strictBindCallApply: Enable strict checking of bind, call, and apply
  • strictPropertyInitialization: Ensure class properties are initialized

Common Type Errors

Error: Type 'X' is not assignable to type 'Y'Cause: You’re trying to assign a value of one type to a variable of another type.Solution: Ensure the types match or use type assertions/conversions.
// ❌ Error
const count: number = "hello";

// ✅ Fixed
const count: number = 42;
Error: Property 'X' does not exist on type 'Y'Cause: You’re accessing a property that doesn’t exist on the object’s type.Solution: Add the property to the type definition or use optional chaining.
// ❌ Error
interface User {
  name: string;
}
const user: User = { name: "John" };
console.log(user.age);

// ✅ Fixed
interface User {
  name: string;
  age?: number;
}
const user: User = { name: "John" };
console.log(user.age);
Error: Parameter 'X' implicitly has an 'any' typeCause: TypeScript can’t infer the type and no explicit type is provided.Solution: Add explicit type annotations.
// ❌ Error
function greet(name) {
  return `Hello, ${name}`;
}

// ✅ Fixed
function greet(name: string): string {
  return `Hello, ${name}`;
}
Error: Object is possibly 'null' or 'undefined'Cause: You’re accessing a property on a value that might be null or undefined.Solution: Add null checks or use optional chaining.
// ❌ Error
function getLength(text: string | null) {
  return text.length;
}

// ✅ Fixed
function getLength(text: string | null) {
  return text?.length ?? 0;
}
Error: Function lacks ending return statementCause: A function with a non-void return type doesn’t return a value in all code paths.Solution: Ensure all code paths return a value.
// ❌ Error
function getValue(flag: boolean): number {
  if (flag) {
    return 42;
  }
  // Missing return for else case
}

// ✅ Fixed
function getValue(flag: boolean): number {
  if (flag) {
    return 42;
  }
  return 0;
}

Integration with Development Workflow

Pre-Commit Hook

Add type checking to your pre-commit hooks:
{
  "husky": {
    "hooks": {
      "pre-commit": "native-desktop types && native-desktop lint"
    }
  }
}

CI/CD Pipeline

Include type checking in your continuous integration:
# GitHub Actions example
- name: Type Check
  run: native-desktop types

npm Scripts

Add type checking to your package.json scripts:
{
  "scripts": {
    "typecheck": "native-desktop types",
    "test": "native-desktop types && npm run test:unit",
    "validate": "native-desktop types && native-desktop lint && native-desktop fmt"
  }
}

Exit Codes

The types command uses standard exit codes for CI/CD integration:
  • 0: No type errors found
  • 1: Type errors found
native-desktop types
echo $?  # Check exit code

Common Issues

Error: Cannot find tsconfig.jsonSolution: Ensure the configuration file exists in your project root.
# Check if file exists
ls -la tsconfig.json

# If missing, create a basic configuration
npx tsc --init
Error: Cannot find module 'xyz'Solution: Install missing type definitions or dependencies.
# Install missing types
npm install --save-dev @types/node

# Or install the actual package
npm install xyz
Error: Overwhelmed by type errors in a new projectSolution: Fix errors gradually or adjust strictness settings temporarily.
{
  "compilerOptions": {
    "strict": false,  // Temporarily disable strict mode
    "noImplicitAny": false
  }
}
Error: Conflicting type definitionsSolution: Check for duplicate type packages or version mismatches.
# List installed type packages
npm list @types

# Remove duplicates
npm uninstall @types/xyz
npm install --save-dev @types/xyz@latest
Error: JavaScript heap out of memorySolution: Increase Node.js memory limit or exclude large directories.
# Increase memory limit
NODE_OPTIONS="--max-old-space-size=4096" native-desktop types
Update tsconfig.json:
{
  "exclude": ["node_modules", "bin", "dist", "large-folder"]
}

Customizing Type Checking

You can customize TypeScript’s behavior in tsconfig.json:
{
  "compilerOptions": {
    // Enable strict type checking
    "strict": true,
    
    // Report errors for unused variables
    "noUnusedLocals": true,
    "noUnusedParameters": true,
    
    // Require explicit return types
    "noImplicitReturns": true,
    
    // Check indexed access
    "noUncheckedIndexedAccess": true,
    
    // Skip checking of declaration files
    "skipLibCheck": true,
    
    // Do not emit output
    "noEmit": true
  },
  "include": ["src/**/*"],
  "exclude": ["node_modules", "**/*.spec.ts"]
}

IDE Integration

Most modern IDEs provide built-in TypeScript support:

Visual Studio Code

TypeScript support is built-in. Configure in settings.json:
{
  "typescript.tsdk": "node_modules/typescript/lib",
  "typescript.enablePromptUseWorkspaceTsdk": true,
  "typescript.updateImportsOnFileMove.enabled": "always",
  "editor.codeActionsOnSave": {
    "source.organizeImports": true
  }
}

WebStorm / IntelliJ IDEA

TypeScript support is built-in and enabled by default. Settings → Languages & Frameworks → TypeScript
  • Enable TypeScript Language Service
  • Use project’s TypeScript version

Type Checking Specific Files

While the CLI command checks the entire project, you can check specific files using tsc directly:
# Check specific file
npx tsc --noEmit src/index.ts

# Check specific directory
npx tsc --noEmit src/components/*.ts

# Check with different config
npx tsc --noEmit -p tsconfig.production.json

Watch Mode

For continuous type checking during development, use TypeScript’s watch mode:
# Run TypeScript compiler in watch mode
npx tsc --noEmit --watch
This will continuously check types as you make changes to your code.

Best Practices

Enable Strict Mode

Use "strict": true for maximum type safety and error prevention.

Type Everything

Add explicit type annotations, especially for function parameters and return types.

Check Regularly

Run type checking frequently during development to catch errors early.

Fix Incrementally

When facing many errors, fix them incrementally rather than all at once.

Type Safety Levels

Different levels of type strictness you can configure:

Command Comparison

CommandPurposeAuto-FixType Checking
native-desktop typesCheck TypeScript types❌ No✅ Yes
native-desktop lintCheck code quality⚠️ Limited❌ No
native-desktop fmtFormat code style✅ Yes❌ No
Use all three commands together for comprehensive code quality: types for type safety, lint for code quality, and fmt for consistent formatting.

Development Workflow

Recommended workflow for type-safe development:

Performance Optimization

For large projects, optimize type checking performance:
{
  "compilerOptions": {
    // Skip checking declaration files
    "skipLibCheck": true,
    
    // Use incremental compilation
    "incremental": true,
    
    // Specify output for incremental info
    "tsBuildInfoFile": ".tsbuildinfo"
  },
  "exclude": [
    "node_modules",
    "bin",
    "dist",
    "**/*.spec.ts"
  ]
}