π Runtime type dynamics, validation, reflection, code generation, and robust third-party library wrapping for enhanced type safety.
π A TypeScript library for runtime type safety and dynamic type management.
TyDy (pronounced /ΛtaΙͺ daΙͺ/) is a TypeScript library that provides robust runtime type safety. It is an essential tool for working with dynamic data structures and APIs where data types can be unpredictable.
flowchart TD
subgraph "π TyDy Library"
subgraph "πΌ Dynamic Type Management"
TypeRegistry[TypeRegistry]
end
subgraph "β
Runtime Type Validation"
TypeValidator[TypeValidator]
end
subgraph "π Type Observability"
TypeObserver[TypeObserver]
end
subgraph "π JSON Type Inference"
JsonTypeInferencer[JsonTypeInferencer]
end
end
TypeRegistry -->|Used by| TypeValidator
TypeRegistry -->|Used by| TypeObserver
TypeRegistry -->|Used by| JsonTypeInferencer
TypeValidator -->|Validates| TypeRegistry
TypeObserver -->|Observes| TypeRegistry
JsonTypeInferencer -->|Infers| TypeRegistry
npm install tydy --save
import { TypeObserver } from "tydy";
// Initialize a new observer instance
const observer = new TypeObserver();
// Register event handlers
observer.on("typeAdded", (typeName: string) => {
console.log(`Type added: ${typeName}`);
});
observer.on("typeRemoved", (typeName: string) => {
console.log(`Type removed: ${typeName}`);
});
// Operations to add and remove types, which trigger the event handlers
observer.addType("MyDynamicType");
observer.removeType("MyDynamicType");
import { JsonTypeInferencer, InferableType } from 'tydy';
// Initialize the inferencer
const inferencer = new JsonTypeInferencer();
const result = inferencer.inferNestedTypes({ name: 'Alice', age: 30, address: { street: '123 Elm St' } });
// Check if the result is an object and log its properties
if (result.type === InferableType.Object || result.type === InferableType.NestedObject) {
console.log(result.properties);
}
import { TypeRegistry } from "tydy";
const typeSchema = { name: "string", age: "number" };
const MyDynamicType = TypeRegistry.registerType(typeSchema);
import { TypeValidator } from "tydy";
// Define a complex dynamic type
const MyDynamicType = {
name: "string",
age: "number",
address: {
street: "string",
zipCode: "number"
}
};
// Create an object to validate
const myObject = {
name: "Alice",
age: 30,
address: {
street: "123 Elm St",
zipCode: 12345
}
};
// Validate the object against the dynamic type
const isValid = TypeValidator.validate(myObject, MyDynamicType);
console.log(isValid);
Contributions are welcomed! For more information on how to contribute, please see our contribution guidelines.
This project is licensed under the MIT License. For complete details, see the LICENSE file.