@razinshafayet/typedjs
v0.3.0
Published
Runtime type checking for JavaScript
Downloads
724
Maintainers
Readme
TypedJS
A comprehensive runtime type system for JavaScript with full TypeScript compatibility. Runtime type checking meets the power of TypeScript's type system.
Features
- 🎯 Complete TypeScript Type System: Support for all TypeScript types including unions, intersections, tuples, generics, enums, and utility types
- 🔍 Runtime Type Checking: Catches type errors as your code runs in development mode
- ⚡ Fast Production Mode: Strips all types and checks for native JavaScript performance
- 🚀 Zero Build Step: Run
.tjsfiles directly with thetypedjsCLI - 📦 Modern Types: Full support for Maps, Sets, Tuples, Records, Template Literals, and more
- ✨ Advanced Features: Conditional types, mapped types, type operators (keyof, typeof)
Installation
npm install -g @razinshafayet/typedjsUsage
Development Mode (with Runtime Validation)
Full runtime type checking - catches bugs TypeScript misses:
typedjs app.tjsProduction Mode (Optimized)
Strips all types and runtime checks for maximum performance:
typedjs app.tjs --prodComprehensive Type Support
Basic Types
let name: string = "Razin";
let age: number = 25;
let isActive: boolean = true;
let empty: null = null;
let notDefined: undefined = undefined;
let big: bigint = 9007199254740991n;
let sym: symbol = Symbol("id");Advanced Primitives
let anyValue: any = "anything";
let unknownValue: unknown = getValue();
let neverReturns: never; // For functions that never return
function log(): void { console.log("void"); }Interfaces & Objects
interface User {
id: number;
name: string;
email?: string; // Optional property
readonly created: Date; // Readonly property
}
let user: User = {
id: 1,
name: "Razin",
created: new Date()
};Type Aliases, Unions & Intersections
// Type alias
type ID = string | number;
// Union types
type Status = "active" | "inactive" | "pending";
// Intersection types
type Named = { name: string };
type Aged = { age: number };
type Person = Named & Aged;
let myId: ID = 123;
let status: Status = "active";
let person: Person = { name: "Alice", age: 30 };Arrays & Tuples
// Arrays
let scores: Array<number> = [10, 20, 30];
let names: string[] = ["Alice", "Bob"];
let readonly: ReadonlyArray<number> = [1, 2, 3];
// Tuples
let point: [number, number] = [10, 20];
let labeled: [x: number, y: number] = [5, 10];
let optional: [string, number?] = ["test"];
let rest: [string, ...number[]] = ["items", 1, 2, 3];Collections
// Map
let userMap: Map<string, number> = new Map();
userMap.set("alice", 30);
// Set
let uniqueIds: Set<number> = new Set([1, 2, 3]);
// Record
type Ages = Record<string, number>;
const ages: Ages = { alice: 30, bob: 25 };Enums
// Numeric enum
enum Color {
Red,
Green,
Blue
}
// String enum
enum Status {
Active = "ACTIVE",
Inactive = "INACTIVE"
}
let color: Color = Color.Red;
let status: Status = Status.Active;Functions
// Basic function
function add(a: number, b: number): number {
return a + b;
}
// Function types
type MathOperation = (a: number, b: number) => number;
// Optional and rest parameters
function greet(name: string, title?: string): string {
return title ? `${title} ${name}` : name;
}
function sum(...numbers: number[]): number {
return numbers.reduce((a, b) => a + b, 0);
}Utility Types
interface Todo {
title: string;
description: string;
completed: boolean;
}
// Make all properties optional
type PartialTodo = Partial<Todo>;
// Make all properties required
type RequiredTodo = Required<Todo>;
// Make all properties readonly
type ReadonlyTodo = Readonly<Todo>;
// Pick specific properties
type TodoPreview = Pick<Todo, "title" | "completed">;
// Omit specific properties
type TodoInfo = Omit<Todo, "completed">;Template Literal Types
type EventName = `on${Capitalize<string>}`;
type Direction = "left" | "right";
type Position = `${Direction}-${number}`;Type Operators
// keyof
type UserKeys = keyof User; // "id" | "name" | "email" | "created"
// typeof
const config = { host: "localhost", port: 3000 };
type Config = typeof config;
// Indexed access
type UserId = User["id"]; // numberHow It Works
TypedJS provides a complete type checking system:
- Parser: Uses
acorn-typescriptto parse TypeScript syntax in.tjsfiles - Analyzer: Performs static analysis to catch type errors before runtime
- Generator:
- Dev Mode: Injects comprehensive runtime type checks
- Prod Mode: Strips all types and generates optimized JavaScript
Development vs Production
Development Mode (typedjs app.tjs):
- Full runtime validation
- Catches type mismatches at runtime
- Validates API responses, user input, external data
- Detailed error messages
Production Mode (typedjs app.tjs --prod):
- All type annotations removed
- No runtime overhead
- Same performance as vanilla JavaScript
- Optimized for deployment
VS Code Extension
Get the full TypedJS experience with syntax highlighting, type validation, and IntelliSense:
code --install-extension razinshafayet.typedjs-vscodeFeatures:
- Full TypeScript syntax highlighting
- Real-time type validation
- IntelliSense for all types
- Hover information
- Code snippets
Why TypedJS?
TypeScript catches bugs at compile time but types disappear at runtime.
TypedJS catches bugs at compile time AND validates types at runtime.
Perfect for:
- API response validation
- User input validation
- External data validation
- Development debugging
- Type-safe JavaScript without build steps
Examples
See the /examples directory for comprehensive examples of:
- Interface usage
- Type aliases and unions
- Enums
- Generic functions
- Utility types
- Real-world patterns
Contributing
Contributions welcome! See CONTRIBUTING.md for guidelines.
License
MIT License - see LICENSE file for details.
Links
Made with ❤️ by Razin Shafayet
