1.0.1 • Published 1 year ago
neuer-typesafe v1.0.1
Neuer Typesafe
Neuer Typesafe is a lightweight JavaScript library for type-safe variable management in both global and scoped environments. It provides tools to define variables with specific configurations like nullable, immutable, and warnOnMismatch, enhancing type safety in your applications.
Installation
Use it directly via a CDN:
<script src="https://cdn.jsdelivr.net/npm/neuer-typesafe/dist/typesafe.min.js"></script>Features
- Type-safe variable management: Define variables with enforced type safety.
- Support for nullable and immutable variables.
- Warnings for type mismatches during reassignment.
- Works with both global and scoped objects.
- Static and strict schema support in extended classes.
Usage
Global Variables
Define type-safe global variables directly on the window object:
window.safeVar`globalVar:nullable`; // Available globally
safeLet`globalLet:nullable`; // Warnings for type changes
safeConst`globalConst`; // Immutable constant
// Assign values
globalVar = "Hello, World!"; // Throws an error if type changes after the first assignment
globalLet = 2; // Console warning if type changes
globalConst = 'OK'; // Immutable, cannot be reassignedScoped Variables
Attach type-safe variables to objects or class instances:
class SafeModel extends BaseModel {
constructor(...args) {
super(...args);
console.log(this);
this.safeVar`scopedVar:nullable`; // Injected via Object.prototype
this.scopedVar = 4; // Scoped variable
console.log(this.scopedVar);
}
static schema = {
id: 'number',
name: 'string'
};
static strict = true;
}safeVar: Define a variable that can be nullable.safeLet: Define a variable with warnings on type mismatches.safeConst: Define an immutable variable.
Injecting TypeSafe to Any Object
Attach the type-safe functionality to any object:
const obj = {};
obj.typeSafe();
obj.safeVar`customVar:nullable`;
obj.customVar = "Value"; // Type-safe assignmentAPI
Global API
safeVar: Define a variable with type safety, optionalnullableconfiguration.safeLet: Define a variable with warnings on type changes.safeConst: Define an immutable constant variable.
Modifiers
nullable: Allow null values.immutable: Prevent reassignment.warnOnMismatch: Log warnings for type mismatches without throwing errors.
License
ISC